gpt4 book ai didi

java - 如何在java中写入二进制文件

转载 作者:行者123 更新时间:2023-11-30 08:50:08 25 4
gpt4 key购买 nike

我怎样才能使最终文件成为二进制文件,而不是 ASCII 文件,所以如果我在文本编辑器中打开 file.bin,它无法读取其中包含的内容。下面的类负责控制写入和读取文件中的arraylist。

代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com