IO流是Java中数据传输的通道,根据传输数据的不同可以分为:字节流(InputStream、OutputStream)和字符流(Reader、Writer),根据流向的不同可以分为:输入流(InputStream、Reader)和输出流(OutputStream、Writer),此外还有缓冲流(Buffre+流)和转换流(InputStreamReader、OutputStreamWriter)。输入流只能读,输出流只能写。
字节输入流的基类是InputStream,我们不能直接使用它,FileInputStream和BufferedInputStream是其使用得最多的子类:
FileInputStream:
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
//一次读取一个字节
try {
FileInputStream in = new FileInputStream(file);
int len;
while ((len = in.read()) != -1) {
Log.i(TAG, "input:" + String.valueOf((char) len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//一次读取一定数量的字节并存入byte数组中
try {
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
//或者
// int size = inputStream.available();
// byte[] bytes = new byte[size];
int len;
while ((len = inputStream.read(bytes)) != -1) {
String str = new String(bytes, 0, len);
Log.i(TAG, "byte input:" + str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream:字节缓冲流
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
int len;
byte[] bytes = new byte[1024];
while ((len = bufferedInputStream.read(bytes)) != -1) {
Log.i(TAG, "input:" + new String(bytes, 0, len));
}
bufferedInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
字节输出流的基类是OutputStrean,FileOutputStream和BufferedOutputStream是其使用最多的子类。
FileOutputStream:
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
FileOutputStream out = new FileOutputStream(file);
out.write("ABCD".getBytes());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//追加写入内容
try {
FileOutputStream appendOut = new FileOutputStream(file, true);
appendOut.write("12345".getBytes());
appendOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedOutputStream:带缓冲的输出流
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
FileOutputStream outputStream = new FileOutputStream(file,true);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
bufferedOutputStream.write("ABCD".getBytes());
bufferedOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
字符输入流的基类是reader,FileReader和InputStreamReader是其使用最多的子类。
FileReader:
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
Reader reader = new FileReader(file);
int count = 0;
char[] ch = new char[1024];
while ((count = reader.read(ch)) != -1) {
Log.i(TAG, new String(ch, 0, count));
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStreamReader:装换流(字节输入流装换为字符输入流)
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
InputStream inputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int len;
char[] chars = new char[1024];
while ((len = inputStreamReader.read(chars)) != -1) {
Log.i(TAG, "inputStreamReader:" + new String(chars, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
字符输出流的基类是Writer,FileWriter和OutputStreamWriter是其使用最多的子类。
FileWriter:
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
Writer writer = new FileWriter(file, true);
writer.append("123");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
OutputStreamWriter:装换流(字节输出流转换为字符输出流)
String path = Environment.getExternalStorageDirectory() + "/Pictures";
File file = new File(path + "/012.txt");
try {
OutputStream outputStream = new FileOutputStream(file,true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write("00000");
outputStreamWriter.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}