一、FileOutputStream
FileOutputStream(File file);
FileOutputStream(String name);
FileOutputStream o = new FileOutputStream();//在springboot中如果只是写个文件名,那么它会生成在pom.xml同级目录下
如果指定了绝对路径就会生成绝对路径下
o.writer(97);输入的是ACSII码中的a
byte[] bytes = {1,2,3,4};
o.writer(bytes);//输出的也是ACSII码中的值
o.writer(bytes,0,2);从0开始写两个
对于追加写入,只要在构造参数中加入第二个参数为true即可
FileOutputStream写出数据时加上异常处理
FileOutputStream fos = null;
try{
fos = new FileOutputStream("a.txt");
fos.writer("java".getBytes());
}catch(FileNotFoundException e){
e.
}catch(IoException e){
e.
}
FileOutputStream和FileInputStream的读写(这里的缓冲区可以不要,因为缓冲区使用的是装饰者模式将inputStream读取的数据保存在内存中,加快读取效率)
FileInputStream in = new FileInputStream("a.pdf");
BufferedInputStream bufferedInputStream = new BufferedInputStream(in);
FileOutputStream out = new FileOutputStream(path +s +".pdf");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while((len=bufferedInputStream.read(buffer))!=-1){
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
bufferedOutputStream.write(buffer, 0, len);
}
//关闭输入流
in.close();
//关闭输出流
out.close();
二、String类中的编码和解码
String(byte[] bytes,String charsetName)//按指定的字符集解码成字符串
byte[] getBytes(String charsetName);//按指定的字符集编码成字节
三、转换流(适配器模式)
OutputStreamWriter
OutputStreamWriter(OutputStream out);根据默认的编码集把字节流转换成字符流
OutputStreamWriter(OutputStream out,String charsetName);根据指定的编码集把字节流转换成字符流
InputStreamReader(InputStream in);
InputStreamReader(InputStream in,String charsetName);
四、字符流的5种写数据方式
public void write(int c);
write(char[] chars);
write(char[] chars,int off,int len);
write(String str);
write(String str,int off,int len)
使用以上方法写的时候写进去不显示,因为文件存储基本单位是字节需要flush()
使用字符流读取数据的方式
FileReader fileReader = new FileReader("a.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
FileWriter f = new FileWriter("b.txt",true);
BufferedWriter bufferedWriter = new BufferedWriter(f);
String s ;
while ((s= bufferedReader.readLine())!=null){
bufferedWriter.write(s);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
不使用缓冲流读取
//int s;
/* while ((s=fileReader.read())!=-1){
f.write(s);
}
f.flush();
fileReader.close();
f.close();*/
五、内存操作流
int a = 0;
int b = 1;
int c = 2;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.write(a);
bout.write(b);
bout.write(c);
byte[] buff = bout.toByteArray();
for (int i = 0; i < buff.length; i++)
System.out.println(buff[i]);
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
while ((b = bin.read()) != -1) {
System.out.println(b);
}
FileInputStream inputStream = new FileInputStream("a.txt");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte [] buffer = new byte[3];
int len = 0;
while( (len = inputStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
byte [] data = outStream.toByteArray();
//FileOutputStream fileOutputStream = new FileOutputStream("c.txt");
//byte [] data = "java".getBytes();
//ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
//byte buffer1[] = new byte[1024];
//int b =0 ;
// while ((b = byteArrayInputStream.read(byffer1)) != -1) {
//System.out.println(b);
//fileOutputStream.write(buffer1,0,b);
// }
//fileOutputStream.close();
System.out.println(new String(data));
/* FileInputStream in = new FileInputStream("a.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(in);*/
FileOutputStream out = new FileOutputStream("b.txt");
//BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
/* byte [] data = "java".getBytes();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while((len=byteArrayInputStream.read(buffer))!=-1){
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
out.write(buffer, 0, len);
}*/
//关闭输入流
//in.close();
//关闭输出流
//out.close();
}