Read and write files, read binary files, bin files

Read and write files, read binary files, bin files

jokewinl  2019-03-27 10:12:30   4616   Collection 2

Transfer from:  https://blog.csdn.net/jokewinl/article/details/88837791

Category Column:  Java  Article Tag:  IO

copyright

Read ordinary files

			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

Read binary file

				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

Write file

				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();

Guess you like

Origin blog.csdn.net/u010689853/article/details/110957360