读写文件,读二进制文件,bin文件

读普通文件

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

读二进制文件

				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源数组,截取起始位置,截取后存放的数组,截取后存放的数组起始位置,截取数组长度

写入文件

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

你可能感兴趣的:(Java)