简言:我们都知道现在使用最高的语言就是java语言了,而想掌握这么语言,有几个知识点是非常必要的,比如java中的网络操作、多线程、io流了。而我们这次就讲一讲io流中最常用的两个,字符流和字节流。
字符流:见名知意,我们可想而知这个是用来对字符数据的操作,比如文本数据等的操作,其实也可以使用字节流(下面会介绍),我们都或多或少的知道我们的字符是指计算机中使用的文字和符号,比如1、2、3、A、B、C、~!·#¥%……—*()——+、等等。他们在不同的编码下所占用的字节数不一样。所以字符数据使用字节流来操作的话可能会出现乱码。
下面就用代码来写字符流的操作
public class 字符流 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("c:/a/zifuliu.txt");//读取该url里面的文件或文件夹
write(f);
red(f);
}
public static void write(File f){
//不存在就创建该文件或文件夹
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ok");
}
try {
//进行文件的写入操作,其实后面有两个参数(File f,boolean b),如果为true代表可以重复写入而不会被覆盖。
FileWriter fw=new FileWriter(f);
//写入你想要的数据到该文件里面
fw.write("haha");
fw.flush();//刷新缓存区,如果不刷新的话数据不会写入文件里面。
fw.close();//关闭操作,节省资源
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void red(File f){
try {
//读取该文件
Reader r=new FileReader(f);
char a[]=new char[1];//因为是字符操作,所以就需要使用字符数组进行读取操作。
//用来存取读出来的数据,至于为什么要使用这个我上一个博客讲过
StringBuffer sb=new StringBuffer();
int len;
try {
//判定数据是否结束了
while((len=r.read(a))!=-1){
sb.append(new String(a,0,len));
}
System.out.println(sb);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
**字节流的操作:**
outputstream字节输出流,要输出使用fileoutputstream。
fileinputstream字节输入流,从文件系统中的的某个文件中获得输入字节。
**步骤:写数据(也就是输出流):**
file f=new file("文件路径以及名字(比如c:/a.txt)");
先判断一下是否存在该文件,不存在就创建该文件。
然后在进行outputstream out=new fileoutputstream(f);该里面有两个参数(file f,boolean b);b为true代表写入不覆盖
String s="内容";然后进行写入即out.write(s.getbytes()),因为是字节,所以要转化为字节。
最后还有关闭out.close
**步骤:读数据(也就是inputstream):**
file f=new file("文件路径以及名字(比如c:/a.txt)");
inputstream in=new fileinputstream(f);
有几种方法进行读取,第一种就是一个字节一个字节的读取(速度太慢),第二种就是一起读取(万一数据下的话,也不适应),第三种就是指定一次读取固定的大小(通常使用这个)。所以说资格byte选取的大小并不是越大越好或者越小越好的,而是去适中最好。
byte b[]=new byte[1024];//代表每次读取1kb
int len=-1;//每次真实读取的长度,最大就是1kb、
Stringbuffer sb=new stringbuffer();
while((len=in.read(b)!=-1){//读取到-1代表读完了
sb.appen(new string(b,0,len));
}
in.close,使用完后进行关闭,释放资源。
该操作主要适用于图片,视频、音乐等媒体文件。
**代码:**
public class 字节流 {
private static File f;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("c:/a/a.txt");
write(f);
red(f);
}
private static void red(File f) {
// TODO Auto-generated method stub
try {
InputStream in=new FileInputStream(f);
//这样也可以实现读取
StringBuffer sb=new StringBuffer();
int len=-1;
byte b[]=new byte[1024];
try {
while((len=in.read(b))!=-1){
sb.append(new String(b,0,len));
}
System.out.println(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*这种方法虽然可以,但是有不能完整的读取数据,比如如果文件里面有换行之类的,他不会显示换行,而是一直读下去。
BufferedReader br=new BufferedReader(new InputStreamReader(in));
StringBuffer s=new StringBuffer();
int len;
String a;
try {
while((a=br.readLine())!=null){
s.append(a);
}
System.out.println(s.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void write(File f) {
// TODO Auto-generated method stub
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ok");
}
try {
OutputStream out=new FileOutputStream(f,true);
try {
out.write("hellow\r\n".getBytes());
out.flush();
out.write("\r\n".getBytes());
out.write("hellow".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}