简单整理一下IO的知识,如有问题还望指出。
流根据不同维度可以分为 输入流和输出流 、字节流和字符流;输入流为读取信息,输出流为写入信息;字节流是以字节为单位读取,字符流是按字符为单位读取(可以是一个汉字也可以是一个字母、数字、符号等);一般使用字节流比较多,字符流个人认为应该是在按字符处理的时候使用,其他情况均可使用字节流。
*简单说明一下 位(bit)、字节(byte)、字符(char)之间的关系
1byte = 8bit;
1字符 = 1byte 或 2byte;
下面整理了一下IO的相关操作方法,里面大概的基础知识点都可以覆盖,代码如下:
package com.sr;
import java.io.*;
/**
* Created by sr on 2018/7/21.
*/
public class IO {
private static final String fileName = "qianming.lic";
public static void main(String[] args) {
byte[] bytes1 = getBytesByFileName1(fileName);
byte[] bytes2 = getBytesByFileName2(fileName);
byte[] bytes3 = getBytesByFileName3(fileName);
System.out.println(bytes1.length);
System.out.println(bytes2.length);
System.out.println(bytes3.length);
InputStream inputStream = getInputStreamByString(fileName);
byte[] bytes4 = getBytesByInputStream(inputStream);
System.out.println("bytes4");
System.out.println(new String(bytes4));
String string1 = getStringByFileName(fileName);
System.out.println("string1");
System.out.println(string1);
String string2 = getStringByFileName2(fileName);
System.out.println("string2");
System.out.println(string2);
String string3 = getStringByFileName3(fileName);
System.out.println("string3");
System.out.println(string3);
file2file(fileName,"testNewFile");
}
/**
* 读取文件获取字节数组1
* @param fileName
* @return
*/
public static byte[] getBytesByFileName1(String fileName){
try {
InputStream inputStream = new FileInputStream(fileName);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int data = 0;
while ((data = inputStream.read()) != -1){
byteArrayOutputStream.write(data);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 读取文件获取字节数组2
* @param fileName
* @return
*/
public static byte[] getBytesByFileName2(String fileName){
try {
InputStream inputStream = new FileInputStream(fileName);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int readLength = 1024;
byte[] bytes = new byte[readLength];
int len = 0;
while ((len = inputStream.read(bytes,0,readLength)) != -1){
byteArrayOutputStream.write(bytes,0,len);
}
return byteArrayOutputStream.toByteArray();//byteArrayOutputStream.toString() 可以直接返回字节数组转换的字符串
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 读取文件获取字节数组3
* @param fileName
* @return
*/
public static byte[] getBytesByFileName3(String fileName){
try {
InputStream inputStream = new FileInputStream(fileName);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int readLength = 200;
byte[] bytes = new byte[readLength];
int len = 0;
while ((len = inputStream.read(bytes)) != -1){
// byteArrayOutputStream.write(bytes);//可能导致写入的长度多
byteArrayOutputStream.write(bytes,0,len);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将输入流转换为字节数组
* @param inputStream
* @return
*/
public static byte[] getBytesByInputStream(InputStream inputStream){
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int readLength = 1024;
byte[] bytes = new byte[readLength];
int len = 0;
while ((len = inputStream.read(bytes,0,readLength)) != -1){
byteArrayOutputStream.write(bytes,0,len);
}
return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 将字符串转为输入流
* @param str
* @return
*/
public static InputStream getInputStreamByString(String str){
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
return inputStream;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 字节流读取文件内容
* @param fileName
* @return
*/
public static String getStringByFileName(String fileName){
try {
return new String(getBytesByFileName2(fileName));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 字符流读取文件内容
* @param fileName
* @return
*/
public static String getStringByFileName2(String fileName){
try {
InputStream inputStream = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
String string = "";
int i = inputStreamReader.read();
while (i != -1) {
string += ((char)i);
i = inputStreamReader.read();
}
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 字符流读取文件内容 按行读
* @param fileName
* @return
*/
public static String getStringByFileName3(String fileName){
try {
InputStream inputStream = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String string = null;
StringBuffer stringBuffer = new StringBuffer();
boolean isFirst = true;
while ((string = bufferedReader.readLine()) != null) {
if (!isFirst){
stringBuffer.append(System.getProperty("line.separator"));//添加换行符
}
isFirst = false;
stringBuffer.append(string);
}
return stringBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 字节流复制文件并写入文件
* 复制文件
* @param oldFileName
* @param newFileName
*/
public static void file2file(String oldFileName,String newFileName){
try {
FileInputStream fileInputStream = new FileInputStream(oldFileName);
FileOutputStream fileOutputStream = new FileOutputStream(newFileName);
int data = 0;
while ((data = fileInputStream.read()) != -1){
fileOutputStream.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}