java IO通过字节流,字符流 读出写入

时间:2023-03-09 18:18:59
java IO通过字节流,字符流 读出写入

一:通过字节流操作数据的写入,读出

	/**
* 通过字节流写入和读出
* @param args
*/
public static String filePath = "G:" + File.separator + "JAVA"
+ File.separator + "test" + File.separator + "aaa.txt"; public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
wirteInByte();
wirteInByteByByte();
wirteInByteAppend(); readFileExitSpace();
readFileNoSpace();
getLengthOfFile();
readByByte();
readToEndofFile(); } /*
* 0. 获取写入文件的两种方法
*/
public static void getFileMethod() throws FileNotFoundException {
File file = new File(filePath);
OutputStream os = new FileOutputStream(file); OutputStream os2 = new FileOutputStream(filePath);
} /*
* 1.向文件中写入整个字节数组
*/
public static void wirteInByte() throws IOException {
//
File file = new File(filePath);
if (!file.exists())
file.createNewFile();
OutputStream os = new FileOutputStream(file); // outputstream
// 是fileoutputstream的抽象类
// 也可直接用 OutputStream os=nmew FileOutputStream(filePath)
String s = "nihaoma";
byte[] myByte = s.getBytes();
os.write(myByte);
os.close();
} /*
* 2.向文件中一个字节一个字节的写入文件
*/
public static void wirteInByteByByte() throws IOException {
File file = new File(filePath);
if (!file.exists())
file.createNewFile(); OutputStream os = new FileOutputStream(file); // outputstream
// 是fileoutputstream的抽象类
String s = "还是键盘敲代码爽啊";
byte[] myByte = s.getBytes();
for (int i = 0; i < myByte.length; i++) {
os.write(myByte[i]);
} os.close();
} /*
* 3.向文件追加文字
*/
public static void wirteInByteAppend() throws IOException {
File file = new File(filePath);
if (!file.exists())
file.createNewFile(); OutputStream os = new FileOutputStream(file, true); // outputstream
// 是fileoutputstream的抽象类
// FileOutputStream(File file, boolean append) 创建一个向指定 File
// 对象表示的文件中写入数据的文件输出流。 String s = "今天的天气有点坑爹啊,怎么办呢?";
byte[] myByte = s.getBytes();
os.write(myByte);
os.close();
} /*
* 4.读取文件的内容(输出会有空格)
*/
public static void readFileExitSpace() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); byte[] allByte = new byte[100];
in.read(allByte);
in.close();
System.out.println(new String(allByte)); // allByte.toString()只是将地址转化为10机制而已
// 这样读取时有很多空格的,因为在byte数组中,只有一部分有值,看下面的方法
} /*
* 5.读取文件的内容(输出会无空格)
*/
public static void readFileNoSpace() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); byte[] allByte = new byte[100];
int len = in.read(allByte); // 注意这里in.read 返回的是读取的长度
in.close();
System.out.println(new String(allByte, 0, len)); // 这样读取时有很多空格的,因为在byte数组中,只有一部分有值,看下面的方法 } /*
* 6.通过file.length 获取对象file的字节长度
*/
public static void getLengthOfFile() {
File file = new File(filePath);
long len = file.length();// 直接可以获取到file的字节长度(必须是问价类型)\
System.out.println("file 问价的字节长度是:" + len); } /*
* 7.一次读取一个字节
*/
public static void readByByte() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); // 获取file字节的大小
int len = (int) file.length();
byte[] allbyte = new byte[len];
for (int i = 0; i < len; i++) {
allbyte[i] = (byte) in.read();
}
in.close();
System.out.println(new String(allbyte));
} /*
* 8.不知道文件的字节大小,只能判断是否读到文件的结尾(提醒一下,当独到文件末尾的时候会返回-1.正常情况下是不会返回-1的)
*/
public static void readToEndofFile() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); byte[] all = new byte[1024];
int count = 0;
int temp =0;
while ((temp=in.read()) != (-1)) {
all[count++] = (byte)temp;
}
in.close();
System.out.println(new String(all, 0, count)); }

二:通过字符流控制字符的读出,写入

	/**
* 字符流测试
* @param args
*/
public static String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
readAll();
readByByte();
insertCharactor();
} /*
*1.从头开始读取所有的字符
*/
public static void readAll() throws IOException
{
File file=new File(filePath);
Reader reader=new FileReader(file);
int len=(int)file.length(); //获取文件 长度
char[] all=new char[len]; reader.read(all);
reader.close();
System.err.println(new String(all));
} /*
* 2.最好一个个读取,判断最后一个字符不是-1
*/
public static void readByByte() throws IOException
{
File file=new File(filePath);
Reader reader =new FileReader(file);
//声明 一个字符型数组
char[] all=new char[1024]; int count =0;
int temp=0;
//读取的到最后一个字符
while((temp=reader.read())!=(-1))
{
all[count++]=(char)temp; }
reader.close();
System.out.println(new String(all,0,count)); } /*
* 3.写操作,直接写入字符串(直接append )
*/
public static void insertCharactor() throws IOException
{
File file=new File(filePath);
Writer writer=new FileWriter(file); String s="晕啊";
writer.write(s);
writer.append("nimeimie");
writer.close();
} }

总结:字符流和字节流的区别和联系

实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。

读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。

使用字节流好还是字符流好呢?

答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

三、文件复制,字节流和字符流的相互转化

	  /*
*1.文件复制
*/
public static void copyFile() throws IOException
{
File fileSrc=new File(filePath);
File fileDes=new File("G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"copy.txt");
if(!fileDes.exists())
fileDes.createNewFile(); InputStream in=new FileInputStream(fileSrc);
OutputStream out=new FileOutputStream(fileDes); int count=0;
int temp=0;
while((temp=in.read())!=(-1))
{
out.write(temp);
}
in.close();
out.close(); } /*
* 2.将字节输入流装换成字符输入流,字节输入流转化为字符输入流
*/
public static void byteStreamChangeIntoCharStream() throws IOException
{
File file=new File(filePath);
//OutputStreamWriter 将字节流转化为字符流
Writer writer=new OutputStreamWriter(new FileOutputStream(file));
//通过InputStreamReader 将字节输入流转化成字符输入流
Reader reader =new InputStreamReader(new FileInputStream(file)); }