IO流的读写操作
import java.io.FileInputStream;import java.io.FileOutputStream;
import java.io.IOException;
public class ByteFile {
private String filename;
public ByteFile(String filename)
{
this.filename=filename;
}
public void writeToFile(byte[] buffer) throws IOException
{
FileOutputStream fout=new FileOutputStream(this.filename);
fout.write(buffer);
fout.close();
}
public void readFromFile() throws IOException
{
FileInputStream fin=new FileInputStream(this.filename);
System.out.println(this.filename+": ");
byte[] buffer=new byte[512];
int count=0;
do{
count=fin.read(buffer);
for(int i=0;i<count;i++)
System.out.print(buffer[i]+" ");
System.out.println("count="+count);
}
while(count!=-1);
fin.close();
}
public void copyFile(String filename2)throws IOException
{
FileInputStream fin=new FileInputStream(this.filename);
FileOutputStream fout=new FileOutputStream(filename2);
byte[] buffer=new byte[512];
int count=fin.read(buffer);
while(count!=-1)
{
fout.write(buffer, 0, count);
count=fin.read(buffer);
}
fin.close();
fout.close();
System.out.println("CopyFile from"+this.filename+" to "+filename2);
}
public static void main(String[] args) throws IOException{
byte[] buffer={0,1,2,3,4,5,6,7,8,9};
ByteFile afile=new ByteFile("f:\\test\\aa.txt");
afile.writeToFile(buffer);
afile.readFromFile();
afile.copyFile("f:\\test\\cc\\zz.txt");
}
}
这段代码所操作的文件的写入的文件内容为Ā̂Ԅ܆ई是什么意思啊,求解释?
----------------解决方案--------------------------------------------------------
就是把buffer的内容打印到指定路径的文件上,这里好像有编码问题,你的打印出来的是乱码。
自己在百度下,然后调调
----------------解决方案--------------------------------------------------------