Java 文本文件和二进制文件的读取(转)
一,文本文件的读取?
1,FileInputStream:按字节读取文件?
1.1,导入包?
import java.io.FileInputStream;?
java.io.InputStream;?
import java.io.*;?
1.2,生成一个InputStream对象?
InputStream in = new FileInputStream("myfile//a.txt");//("myfile//a.txt",true),写入的内容追加在原有内容的后面,不覆盖?
1.3,一个一个字节地读取文件内容?
try {?
????? int size = in.available();?
????? char x[] = new char[200];?
????? for (int i = 0; i < size; i++) {?
??????? x[i] = (char)in.read();?
??????? System.out.print(x[i]);?
????? }?
??? }?
??? catch (IOException ex1) {?
????? ex1.printStackTrace();?
??? }?
1.4,关闭对象?
finally{?
????? try {?
??????? in.close();?
????? }?
????? catch (IOException ex2) {?
??????? ex2.printStackTrace();?
????? }?
??? }?
=================================================?
2,FileOutputStream:按字节写入文件?
2.1,导入包?
import java.io.*;?
2.2,声明一个OutputStream引用?
OutputStream out =null;?
2.3,构造一个OutputStream对象,并在其中写入内容?
try {?
???? out = new FileOutputStream("b.txt");?
??? String str ="java终于完了";?
??? byte[] b = str.getBytes();?
??? try {?
????? out.write(b, 0, b.length);?
??? }?
??? catch (IOException ex1) {?
????? ex1.printStackTrace();?
??? }?
2.4,关闭对象?
finally{?
??? try {?
????? out.close();?
??? }?
??? catch (IOException ex2) {?
????? ex2.printStackTrace();?
??? }?
? }?
=================================?
3,BufferedReader:按字符读取内容?
3.1,导入包?
import java.io.*;?
3.2,声明一个FileReader和BufferedReader的引用?
FileReader fr? = null;?
BufferedReader buf =null;?
3.3,构造一个FileReader和BUfferedReader的对象?
fr = new FileReader("myfile//a.txt");?
buf = new BufferedReader(fr);?
3.4,按行读取文件内容?
try {?
??? String s = buf.readLine();?
??? while(s!=null)?
??? {?
????? System.out.println(s);?
????? s =buf.readLine();?
??? }?
? }?
? catch (IOException ex1) {?
??? ex1.printStackTrace();?
? }?
3.5,关闭对象连接?
finally{?
??? try {?
????? buf.close();?
????? fr.close();?
??? }?
??? catch (IOException ex2) {?
????? ex2.printStackTrace();?
??? }?
? }?
====================================?
4,BufferWriter:按字符写入内容?
4.1,导入包?
import java.io.*;?
4.2,声明一个FileWriter和BufferedWriter的引用?
FileWriter fw? = null;?
????? BufferedWriter buf = null;?
4.3,构造一个FileWriter和BUfferedWriter的对象,并写入内容?
try {?
???? fw = new FileWriter("c.txt");?
???? buf = new BufferedWriter(fw);?
??? buf.write("你好!!");?
? }?
? catch (IOException ex) {?
??? ex.printStackTrace();?
? }?
4.4,关闭对象连接?
finally{?
??? try {?
????? buf.close();?
????? fw.close();?
??? }?
??? catch (IOException ex1) {?
????? ex1.printStackTrace();?
??? }?
? }?
====================================?
二,二进制文件的读写?
1,导入包?
import java.io.*;?
2,生成FileInputStream,DataInputStream,FileOutputStream,DataOutputStream的对象?
FileInputStream fin = new FileInputStream("d://x.jpg");?
???? DataInputStream da = new DataInputStream(fin);?
???? FileOutputStream fout = new FileOutputStream("e://b.jpg");?
???? DataOutputStream dout = new DataOutputStream(fout);?
3,写入数据?
?? int temp ;?
??? try {?
????? while ( (temp = da.read()) != -1) {?
?????????????????? dout.write(temp);?
????? }?
??? }?
??? catch (IOException ex1) {?
????? ex1.printStackTrace();?
??? }?
4,关闭连接?
finally{?
??? try {?
fin.close();?
?????? da.close();?
?? fout.close();?
dout.close();?
??? }?
??? catch (IOException ex2) {?
????? ex2.printStackTrace();?
??? }?
? }
5 将文件转成二进制流,然后进行写入数据操作
public class IOTest
{
public static void main(String[] args) throws FileNotFoundException
{
FileOutputStream fout = new FileOutputStream("e:\\2.xls");
DataOutputStream dout = new DataOutputStream(fout);
byte[] buffer = getByteFromFile("c:\\1.xls");
int count, i;
try
{
for (i = 0; i < buffer.length; i++)
{
dout.write(buffer[i]);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
fout.close();
dout.close();
}
catch (IOException ex2)
{
ex2.printStackTrace();
}
}
?
}
?
private static byte[] getByteFromFile(String file)
{
File f = new File(file);
if (f == null)
{
return null;
}
try
{
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray();
}
catch (IOException e)
{
}
return null;
?
}
}