1. 程式人生 > >java中的檔案操作:讀取寫入byte[]位元組流、string字串、list列表

java中的檔案操作:讀取寫入byte[]位元組流、string字串、list列表

全棧工程師開發手冊 (作者:欒鵬)

java中檔案操作:讀取檔案成位元組流,將位元組流寫入檔案,按行讀取檔案成字串列表,將字串列表儲存成檔案,讀取檔案成字串,將字串寫入檔案。

主程式測試檔案

    //測試程式
    public static void main(String[] args) 
    {
                byte[] data=file2byte("test.txt");                  //讀取檔案
        byte2file("test1.txt",data);                        //寫入檔案
ArrayList<String> allline=file2list("test.txt","UTF-8"); //按行讀取檔案 list2file("test2.txt",allline,"UTF-8"); //儲存檔案 String content=file2str("test.txt","UTF-8"); //讀取檔案 str2file("test3.txt",content,"UTF-8"); //儲存檔案
}

讀取檔案成位元組陣列

    public static byte[] file2byte(String path)
    {
        try {
            FileInputStream in =new FileInputStream(new File(path));
            //當檔案沒有結束時,每次讀取一個位元組顯示
            byte[] data=new byte[in.available()];
            in.read(data);
            in.close();
            return
data; } catch (Exception e) { e.printStackTrace(); return null; } }

將位元組陣列寫入檔案

    public static void byte2file(String path,byte[] data) {
        try {
            FileOutputStream outputStream  =new FileOutputStream(new File(path));
            outputStream.write(data);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

按行讀取檔案成list

    public static ArrayList<String> file2list(String path,String encoder) {
        ArrayList<String> alline=new ArrayList<String>();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String();
            while ((str=in.readLine())!=null) {
                alline.add(str);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alline;
    }

輸出list到檔案

    public static void list2file(String path,ArrayList<String> data,String encoder) 
    {
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            for (String str:data) {
                out.write(str);
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

從標準輸入中讀入

    public static String system2str() throws IOException{
        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        return stdin.readLine();
    }

讀取檔案成字串

    public static String file2str(String path,String encoder) 
    {
        StringBuilder sb=new StringBuilder();
        try {
            BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder));
            String str=new String();
            while ((str=in.readLine())!=null) {
                sb.append(str);
            }
            in.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

輸出字串到檔案

    public static void str2file(String path,String data,String encoder) 
    {
        try {
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder));
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

讀取檔案成資料矩陣

    public static ArrayList<Double> file2matrix(String path) 
    {
        ArrayList<Double> alldata=new ArrayList<Double>();
        try {
            DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
            //利用DataInputStream來讀資料
            while(true)
            {
                alldata.add(in.readDouble());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return alldata;
    }

相關推薦

java檔案操作讀取寫入byte[]位元組string字串list列表

全棧工程師開發手冊 (作者:欒鵬) java中檔案操作:讀取檔案成位元組流,將位元組流寫入檔案,按行讀取檔案成字串列表,將字串列表儲存成檔案,讀取檔案成字串,將字串寫入檔案。 主

Java檔案操作

java提供了一些實現類對檔案進行操作 File 對具體檔案(目錄)進行抽象表示。File類只用於表示檔案或者目錄的資訊(名稱、大小),不能用於檔案內容的訪問。 file類的方法比較多,以一個例項演示常用的幾個API。 import java.io.F

ini檔案操作讀取/寫入

示例: // iniText.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include &l

JAVA檔案操作大全

一.獲得控制檯使用者輸入的資訊/**//** *//**//**獲得控制檯使用者輸入的資訊     * @return     * @throws IOException     */    public String getInputMessage() throws IOException......{  

File類的特點?如何建立File類物件?Java如何操作檔案內容,什麼是IoIo如何讀取寫入檔案?位元組緩衝使用原則?

                            重難點提示                                 學習目標 1、能夠了解File類的特點(存在的意義,構造方法,常見方法) 2、能夠了解什麼是IO流以及分類(IO流的概述以及分類)

Java配置檔案Properties的讀取寫入與更新操作

/**   * 實現對Java配置檔案Properties的讀取、寫入與更新操作   */    package test;     &nbs

java檔案內容讀取寫入

對於java檔案讀取一直比較迷糊,整理了下,日後可以直接翻看。 package baixiaosheng; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import ja

Java POI 操作Excel(讀取/寫入)

del sep ces 價值 name fill ber 路徑 stc pom.xml依賴: <dependency> <groupId>org.apache.poi</groupId>

檔案操作如果檔案存在則清空,然後向檔案寫入內容

#include <stdio.h> void fac(int x){ static FILE *fp; static flag=0; if(flag==0){ fp=fopen("coordinate.txt","w+"); flag=1; }else{ fp=f

java對xml的讀取寫入

java對xml操作需匯入dom4j的jar包(如下): (解析)讀取xml: package com.rj.bd.xml.jx; import java.io.File; import java.util.List; import org.dom4j.Attri

Java檔案寫入

package demo1; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; pu

Java之XML操作從XML直接獲取數據

proc arraylist Coding xml文件 art ioe input roc demo   本文介紹如何將數據記錄在XML文件中,然後通過DOM4J直接從XML中讀取到數據。 依賴包: <dependency> <groupId&g

java多執行緒讀取同一個檔案的不同位置,多執行緒讀取檔案

今天遇到一個問題,需要多個執行緒讀取同一個檔案的不同位置,提高效率,寫程式碼驗證了一下,把結果記錄下來。 首先我們寫個檔案,然後多執行緒讀取,以下是我實驗的程式碼: package com.alibaba.middleware.race; im

JAVA檔案工具類之——檔案寫入byte陣列String方式url寫入方式)

/** * 將byte陣列寫入檔案 * * @param path * @param fileName * @param content * @throws I

java 檔案讀取File以及相對路徑的問題

File file01 = new File("config/log4j.properties");System.out.println(file01.getAbsolutePath()); File file02 = new File(properties.getProperty("user.dir")

Python檔案操作從文字末尾逆序讀取資料

class TraceManagerBase(object): dblocation = "" def __init__(self): self.init() def init(self): self.dblocat

javaProperties類及讀取properties屬性值

key ioe failed .cn pre new ava 進行 html 在項目的應用中,經常將一些配置放入properties文件中,在代碼應用中讀取properties文件,就需要專門的類Properties類,通過這個類可以進行讀取。 深入理解和學習的

Javaint類型強轉為byte類型,強轉為byte之後,數據超出byte的表述範圍是如何計算的?

ava -128 div string blog pub 二進制 就是 轉換 public class Aa { public static void main(String[] args) { byte i = 88; byte r = 68; byte f = (

java使用相對路徑讀取文件的寫法總結 ,以及getResourceAsStream() (轉)

protected 9.png pre ring details 使用 ide 技術分享 相對 https://blog.csdn.net/my__sun_/article/details/74450241 讀取文件的寫法,相對路徑 在當前的目錄結構中讀取test.txt的