如何使最终文件为二进制文件,而不是 ASCII 格式,因此,如果我在文本编辑器中打开 file.bin,它无法读取其中包含的内容。 下面的类负责控制文件中数组列表的读写。
代码:
package IOmanager;
import data.ControlData;
import data.Sheet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author lcsvcn
*/
public class IO_Manager {
private static final String src = "src/IOmanager/file.bin";
private ArrayList<Sheet> al = null;
private boolean hasArray;
private ControlData cd;
public IO_Manager(ControlData cd) {
this.cd = cd;
loadArray();
}
private void saveArray() {
if(al.isEmpty()) {
System.out.println("ArrayList is empty");
} else if(al == null) {
System.out.println("ArrayList is null send some array");
} else {
File fout = new File(src);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fout);
} catch (FileNotFoundException ex) {
System.out.println("FileNotFoundException");
Logger.getLogger(IO_Manager.class.getName()).log(Level.SEVERE, null, ex);
}
try {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(src, true)))) {
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
// try {
for(Sheet s : al) {
// Cod Ficha
System.out.println("w: "+ s.getSheetID());
out.println(Integer.toString(s.getSheetID()));
// bw.newLine();
// Nome
System.out.println("w: " + s.getName());
out.println(s.getName());
// bw.newLine();
// Idade
System.out.println("w: " + s.getAge());
out.println(Integer.toString(s.getAge()));
// bw.newLine();
// Plano de Saude
System.out.println("w: "+ s.getHealthyCarePlan());
out.println(Integer.toString(s.getHealthyCarePlan()));
// bw.newLine();
// Estado Civil
System.out.println("w: "+ s.getMaritalStatus());
out.println(Integer.toString(s.getMaritalStatus()));
// bw.newLine();
}
out.close();
} catch (IOException ex) {
System.out.println("IOException");
Logger.getLogger(IO_Manager.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("saved");
} catch (NullPointerException e) {
System.out.println("fucked");
}
}
}
private boolean loadArray() {
File fin = new File(src);
FileInputStream fis = null;
String name="erro";
int age=-1;
int sId=-1;
int hCarePlan=-1;
int mStatus=-1;
int cont = 0;
int num = 5;
int reg = 0;
try {
fis = new FileInputStream(fin);
} catch (FileNotFoundException ex) {
System.out.println("File to load not found!");
return false;
}
//Construct BufferedReader from InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
System.out.println("-------------");
System.out.println("inicio da leitura");
System.out.println("-------------");
String line = null;
try {
System.out.println("Reg num: " + reg);
while((line = br.readLine()) != null) {
switch(cont) {
case 0:
sId = Integer.valueOf(line);
System.out.println("r: " + sId);
break;
case 1:
name = line;
System.out.println("r: " + name);
break;
case 2:
age = Integer.valueOf(line);
System.out.println("r: " + age);
break;
case 3:
hCarePlan = Integer.valueOf(line);
System.out.println("r: " + hCarePlan);
break;
case 4:
mStatus = Integer.valueOf(line);
System.out.println("r: " + mStatus);
break;
}
// Fim de um registro e comeco de outro
if(cont >= num) {
cd.sendData(name, age, mStatus, hCarePlan, sId);
cont = 0;
reg++;
System.out.println("-------------");
System.out.println("Reg num: " + reg);
}
cont++;
}
br.close();
System.out.println("-------------");
System.out.println("fim da leitura");
System.out.println("-------------");
} catch (IOException ex) {
Logger.getLogger(IO_Manager.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("load");
return true;
}
public void setArray(ArrayList<Sheet> mList) {
al = mList;
saveArray();
}
}
最佳答案
将二进制内容写入文件与使用二进制输出流写入内容(例如文本)之间存在很大差异。
例如,如果您有一个像“ABCDEF”这样的字符串,您可以使用 FileWriter
(文本输出流示例)和 FileOutputStream
(示例)将其写入文件二进制输出流)。但这并不意味着如果您打开该文件您会看到不同的输出。
如果您想要二进制输出,您应该将其转换为二进制值,然后写入文件。
还可以使用ObjectOutputStream
将对象写入文件,这使得输出文件接近您想要的内容。
在您提供的代码片段中,您可以将整个 ArrayList 对象写入文件中。它使输出文件的可读性较差。
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(al);
下次当您想阅读它时:
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(src)));
ArrayList<Sheet> allSheets = (ArrayList<Sheet>)in.readObject();
希望这会有所帮助,
祝你好运。
关于java - java中如何以二进制形式写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31130030/