1. 程式人生 > 其它 >讀寫檔案,讀二進位制檔案,bin檔案

讀寫檔案,讀二進位制檔案,bin檔案

技術標籤:筆記java

讀寫檔案,讀二進位制檔案,bin檔案

jokewinl2019-03-27 10:12:304616收藏2

轉自 :https://blog.csdn.net/jokewinl/article/details/88837791

分類專欄:Java文章標籤:IO

版權

讀普通檔案

			FileReader reader = new FileReader(meFileName);
            BufferedReader br = new BufferedReader(reader);
            StringBuilder lines = new StringBuilder();
            while ((line = br.readLine()) != null) {
                // 一次讀入一行資料
                lines.append(line);
            }
            line = lines.toString();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

讀二進位制檔案

				bytesTo = new byte[fileLength.intValue()];
                DataInputStream read = new DataInputStream(new FileInputStream(new File(filePath)));

                read.read(bytesTo);
                read.close();
				
				System.arraycopy(filecontent,(i-1)*SIZEZ,bytes, 0,SIZEZ);//buty源陣列,擷取起始位置,擷取後存放的陣列,擷取後存放的陣列起始位置,擷取陣列長度
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

寫入檔案

				File meFile = new File(mePath);
				if(!meFile.exists()){
                        meFile.createNewFile();
                    }
                    FileWriter fw = new FileWriter(meFile.getAbsoluteFile()); //表示不追加
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write(jsonObject.toString());
                    bw.close();