最近学习了如何读取输出文件,贴上部分代码:
TestInputStream1():
String strFile = "Data/DataGroup.cpp";
File = new File(strFile);
InputStream in = null;
in = new FileInputStream(file);
//创建合适文件大小的数组,一次性把数据从文件中读出
b1 = new byte[(int)file.length()];
// 读取文件中的内容到b[]数组,如果read()返回读取的字节内容,
// 当内容为空时返回-1,可以以此作为判断文件内容是否读取完毕
in.read(b1);
in.close();
textArea.append(new String(b1));
TestInputStream2():
int iSize = 1024;
boolean blSleep = true;
String strFile = "Data/DataGroup.cpp";
File file = new File(strFile);
int iCount = (int)file.length()/iSize;
InputStream in = null;
in = new FileInputStream(file);
b2 =new byte[iSize];
int temp = 0;
int no = 0;
while((temp = in.read()) != -1){
no++;
in.read(b2);
System.out.println(new String(b2));
if(no < iCount){
in.read(b2);
System.out.println(new String(b2));
}
else{
blSleep = false;
System.out.print((char)temp);
}
}
in.close();
执行TestInputStream1(),后:
TestOutputStream(): (读文本文件,然后显示到组件,写到另外一个文件)
// 写文件时的相关变量
String strOut = "*Data/DataGroup_copy.cpp";
File file = new File(strOut);
OutputStream output = null;
output = new FileOutputStream(file);
output.write(b1);
output.close();
TestPrintStream()://读取并输出屏幕上,输出到文件上
String strFile = "Data/DataGroup.cpp";
File file = new File(strFile);
InputStream in = null;
in = new FileInputStream(file);
//创建合适文件大小的数组,一次性把数据从文件中读出
b1 = new byte[(int)file.length()];
// 读取文件中的内容到b[]数组,如果read()返回读取的字节内容,
// 当内容为空时返回-1,可以以此作为判断文件内容是否读取完毕
in.read(b1);
in.close();
// 将数据输出到屏幕
PrintStream printStream = new PrintStream(System.out);
printStream.println(new String(b1));
printStream.flush(); // 彻底完成输出并清空缓冲区
printStream.close();
// 将数据输出到文件
File filePrint = new File("Data/print.cpp");
printStream = new PrintStream(filePrint);
printStream.println(new String(b1));
printStream.close();
TestDataInputStream():
// 键盘输入(如何自适应根据用户输入的数据多少分配空间)
byte b[] = new byte[12];
DataInputStream dataInputStream = new DataInputStream(System.in);
String str = null;
dataInputStream.read(b);
dataInputStream.close();
System.out.println(new String(b));
// 文件输入
String strFile = "Data/表2-5.txt";
byte byFile[] = null;
File file = new File(strFile);
InputStream instream = null;
instream = new FileInputStream(file);
dataInputStream = new DataInputStream(instream);
float temp = (float) 0.0;
str = null;
byFile = new byte[(int)file.length()];
dataInputStream.read(byFile);
dataInputStream.close();
System.out.println(new String(byFile));
TestDataInputStream2()://二进制
System.out.println("从文件中每次读取一个数值");
DataInputStream dataInputStream = null;
// 文件输入
String strFile = "Data/表2-5_Copy.txt";
File file = new File(strFile);
InputStream instream = null;
instream = new FileInputStream(file);
dataInputStream = new DataInputStream(instream);
System.out.println("二进制文件读取结果的输出!");
float temp = (float) 0.0;
// 判断是否文件结束
while(dataInputStream.available() > 0){
temp = dataInputStream.readFloat();
System.out.println(temp);
instream.close();
dataInputStream.close();
TestDataOutputStream():
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
// 文件输入
byte byFile[] = null;
File file = new File("Data/表2-5.txt");
InputStream instream = null;
instream = new FileInputStream(file);
inputStream = new DataInputStream(instream);
String str = null;
byFile = new byte[(int)file.length()];
inputStream.read(byFile);
str = new String(byFile);
inputStream.close();
String strSplit[] = str.split(" ");
System.out.println(strSplit.length);
// 把字符串str中的数据一个一个写入文件中(二进制格式文件)
String strFile = "Data/表2-5_Copy.txt";
file = new File(strFile);
OutputStream out = null;
out = new FileOutputStream(file);
outputStream = new DataOutputStream(out);
for(int i = 0;i < strSplit.length;i++){
float temp = Float.valueOf(strSplit[i]);
System.out.println(temp);
outputStream.writeFloat(temp);
}
out.close();
outputStream.close();
}
TestBufferReaderAndBufferedWriter():
// 键盘输入
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr) ;
String str = null;
str = br.readLine();
isr.close();
br.close();
System.out.println(str);
// 读文件
str = null;
String strFile = "Data/result_表2-5.txt";
if(isr != null) isr = null;
isr = new InputStreamReader(new FileInputStream(new File(strFile)));
br = new BufferedReader(isr);
// 写文件
String strWrite = "Data/result_表2-5_copy.txt";
OutputStreamWriter out = null;
BufferedWriter bw = null;
out = new OutputStreamWriter(new FileOutputStream(new File(strWrite)));
bw = new BufferedWriter(out);
// Java中有没有类似于rewind函数,即对指针进行重定位?????????
isr = new InputStreamReader(new FileInputStream(new File(strFile)));
br = new BufferedReader(isr);
str = null;
while((str = br.readLine()) != null){
System.out.println(str);
bw.write(str); // 在使用readLine读取时,换行符已经被取消
bw.newLine();
}
bw.close();
out.close();