输入输出流的抽象基类
字节输入流-InputStream
字节输出流-OutputStream
适用场景: 字节流适用读入或者写出二进制文件,如图片,视频,音频等
字符输入流-Reader
字符输出流-Writer
适用场景: 字符流适用读入或者写出文本文件,如txt等
字节输入流
常用的实现类
FileInputStream
BufferedInputStream
ObjectInputStream
字节输出流
常见的实现类
FileOutputStream
BufferedOutputStream
ObjectOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package pers.xiaotian.ioproject.io;
import java.io.*;
public class StreamTest {
public static void main(String[] args) { InputStream is = null; OutputStream os = null;
try { os = new FileOutputStream("byte1.txt");
int ascii = 65; System.out.println((char) ascii); os.write(ascii);
String str = "\n中文"; os.write(str.getBytes());
is = new FileInputStream("byte1.txt"); FileInputStream fis = new FileInputStream("1"); fis.getChannel(); System.out.println(is.read()); System.out.println(is.read()); System.out.println(is.read()); System.out.println(is.read()); } catch (IOException e) { throw new RuntimeException(e); }finally { try { is.close(); os.close(); } catch (IOException e) { throw new RuntimeException(e); }
} } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package pers.xiaotian.ioproject.io;
import pers.xiaotian.ioproject.object.Data;
import java.io.*;
public class StreamTest2 {
public static void main(String[] args) { OutputStream os = null; InputStream is = null; ObjectOutputStream oos = null; ObjectInputStream ois = null; try { os = new FileOutputStream("byte2.txt"); oos = new ObjectOutputStream(os);
Data data = new Data(); data.setDesc("描述信息"); data.setName("张三");
Data data1 =new Data(); data1.setDesc("描述信息1"); data1.setName("李四");
oos.writeObject(data); oos.writeObject(data1); oos.flush();
FileInputStream is1 = new FileInputStream("byte2.txt"); ObjectInputStream ois1 = new ObjectInputStream(is1); byte[] datas = new byte[2048]; int len = 0; while((len =ois1.read(datas))!=-1){ System.out.println(new String(datas, 0, len)); }
is = new FileInputStream("byte2.txt"); ois = new ObjectInputStream(is);
System.out.println(ois.readObject());
} catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); }finally { try { is.close(); os.close(); oos.close(); ois.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package pers.xiaotian.ioproject.io;
import java.io.*;
public class StreamTest3 {
public static void main(String[] args) { InputStream is = null; OutputStream os = null; BufferedOutputStream bos = null; BufferedInputStream bis = null;
try { os = new FileOutputStream("byte3.txt"); is = new FileInputStream("byte3.txt");
bos = new BufferedOutputStream(os); String str = "asfuashdfha阿花复读啊好烦的安抚爱上发送到插上发爱上对方爱上发"; bos.write(str.getBytes()); bos.flush(); byte[] ins = new byte[1024]; bis = new BufferedInputStream(is); int len = 0; while ((len = bis.read(ins)) != -1){ System.out.println(new String(ins,0, len)); } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); }finally { try { is.close(); os.close(); bos.close(); bis.close(); } catch (IOException e) { throw new RuntimeException(e); } } } }
|
字符输入流
InputStreamReader
FileReader
BufferedReader
字符输出流
OutputStreamWriter
FileWriter
BufferedWriter
FileReader和FileWriter使用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package pers.xiaotian.ioproject.io;
import java.io.*;
public class ReadWriteTest {
public static void main(String[] args) { Writer writer = null; Reader reader = null;
try { writer = new FileWriter("char1.txt"); reader = new FileReader("char1.txt"); String str = "字符写出"; writer.write(str); writer.flush();
char[] chars = new char[1024]; int len; while((len = reader.read(chars))!=-1){ System.out.println(new String(chars,0, len)); }
} catch (IOException e) { throw new RuntimeException(e); }finally { try { writer.close(); reader.close(); } catch (IOException e) { throw new RuntimeException(e); }
}
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package pers.xiaotian.ioproject.io;
import java.io.*;
public class ReadWriteTest2 {
public static void main(String[] args) { Reader reader = null; Writer writer = null; InputStream is = null; OutputStream os = null;
try { os = new FileOutputStream("char2.txt"); writer = new OutputStreamWriter(os); writer.write("写入字符串3"); writer.flush(); is = new FileInputStream("char2.txt"); reader = new InputStreamReader(is); char[] chars = new char["写入字符串3".length()]; int len; while((len = reader.read(chars))!= -1){ System.out.println(new String(chars, 0, len)); } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); }finally { try { reader.close(); writer.close(); is.close(); os.close(); } catch (IOException e) { throw new RuntimeException(e); } }
} }
|
BufferedReader和BufferedWriter使用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package pers.xiaotian.ioproject.io;
import java.io.*;
public class ReadWriteTest3 { public static void main(String[] args) { Reader reader = null; Writer writer = null;
BufferedWriter bw = null; BufferedReader br = null;
try { writer = new FileWriter("char3.txt"); bw = new BufferedWriter(writer); bw.write("字符串写入缓冲区"); bw.flush();
reader = new FileReader("char3.txt"); br = new BufferedReader(reader); System.out.println(br.readLine());
} catch (IOException e) { throw new RuntimeException(e); }finally { try { reader.close(); writer.close(); bw.close(); br.close(); } catch (IOException e) { throw new RuntimeException(e); } }
} }
|
spring项目,需要获取resoutce目录下的文件可以使用以下方法获取:
1
| InputStream file = this.getClass().getClassLoader().getResourceAsStream("testData.xlsx");
|
将输入的文件流转换成MultipartFile可以使用下面方法
1 2
| MultipartFile multipartFile = new MockMultipartFile("test.xlsx", "test.xlsx", null, file);
|
参考博客:
https://www.upstudy.top/index.php/archives/23/