java中I/O的心得笔记(可以使用流的方法复制音频文件)

流主要分为字节流和字符流

InputStream,OutputStream,Reader,Writer,File

可以直接使用的流对象1,System.in(InputStream类)2,System.out(PrintStream类) 3. System.err(PrintStream类)

使用流的步骤,首先需要创建一个与数据有关的流对象,然后利用流对象传输数据,最后关闭流对象。

使用字节流可以读写任何数据,而使用字符流主要是面向文本文件的

1. InputStream
主要使用的子类FileInputStream , BufferedInputStream , DataInputStream
主要方法是public int read(),其中的参数可以无参,可以是字节数组,还可以是字节数组的off和len

import java.io.*;

public class TestIO {
	public static void main(String[] args) {
		try {
			FileInputStream in = new FileInputStream("test.txt");
			byte[] test = new byte[1];
			int flag = in.read(test);
			while(flag != -1) {
				System.out.print(new String(test));
				flag = in.read(test);
			}
			in.close();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}
}

上面的代码可以完成一个文件的读取,当读取到结尾时read方法就会返回一个-1值

关于OutputStream面向字节的流

下面先创建一个int型的数据文件,然后再读取出来

import java.io.*;

public class TestIO {
public static void main(String[] args) {
try {
FileOutputStream file = new FileOutputStream(“1.bat”);
DataOutputStream out2 = new DataOutputStream(file);
int a = 65;
out2.write(a);
out2.close();
FileInputStream file2 = new FileInputStream(“1.bat”);
DataInputStream in = new DataInputStream(file2);
int b = in.read();
System.out.println(b);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

}

}

当我们查看生成的文件的时候,我们会看到的是字符A,这是65这个ascii码代表的字母。而当我们通过键盘输入的是字符,通俗的说就是我们输入的数字不是数字,而是一个个的字符。

Reader便于对于文本文件的读取

import java.io.*;

public class TestIO {
public static void main(String[] args) {
try {
FileReader file = new FileReader(“test.txt”);
// LineNumberReader in = new LineNumberReader(file);
BufferedReader in = new BufferedReader(file);
String s = in.readLine();
while(s != null) {
System.out.println(s);
s = in.readLine();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
这里使用了readLine()方法,使用这个方法可以很方便地逐行读取文本文件,如果使用read()方法可能会比较麻烦一些,但是在对二进制文件操作的时候,较多使用read()方法

Writer写入文本文件

import java.io.*;

public class TestIO {
public static void main(String[] args) {
try {
FileWriter out = new FileWriter(“1.txt”);
byte[] test = new byte[100];
int len = System.in.read(test);
out.write(new String(test,0,len));
out.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

	}

}

最后给出一个可以复制二进制文件的例子,(MP3,视频之类的)

public class TestIO {
public static void main(String[] args) throws IOException{
FileInputStream in = new FileInputStream(“d:\赵雷 - 成都.mp3”);
FileOutputStream out = new FileOutputStream(“1.mp3”);
try {
byte[] temp = new byte[10];
int len = in.read(temp);
while(len != -1) {
out.write(temp, 0, len);
len = in.read(temp);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
out.flush();
out.close();
in.close();
}

}

}

最后我在学习这个二进制文件复制程序的时候参考了 https://blog.csdn.net/sinat_27535209/article/details/80839391的博文

大家加油吧!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

able陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值