废话不多说,直接上代码
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
/**
* @author: 何其有静
* @date: 2019/4/10
* @description:
*/
public class ReadFile {
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* Reader是I/O中读取字符的父类,InputStream是读取字节的父类,InputStreamReader是关联字节到字符的桥梁。
* Writer是I/O中写入字符的父类,OutputStream是写入字节的父类,OutputStreamReader是关联字节到字符的桥梁。
* FileReader extends InputStreamReader extends Reader, BufferedReader extends Reader
* FileWriter extends OutputStreamWriter extends Writer, BufferedWriter extends Writer
*
java的IO流分两种流
字节流 InputStream OutputStream
字符流 Reader Writer
他们都是抽象类
具体实现
字节流 FileInputStream FileOutputStream
字符流 FileReader FileWriter
*/
public static void main(String[] args) throws IOException {
getInputStream();//InputStream读取字节
getOutputStream();//OutputStream写入字节
getBufferReaderAndWriter();//BufferReader和BufferWriter读取字符和写入字符
getOutAndInputStreamReader();//InputStreamReader和OutputStreamWriter的用法
getnewBufferedReaderAndWriter();//newBufferedReader和newBufferedWriter的用法
Convert();//对象转化为json,json转化为map
}
//读取字节流InputStream
public static void getInputStream() throws IOException {
String path = "D:/1.txt";
FileInputStream inputStream = new FileInputStream(path);
byte[] data = new byte[1024];
inputStream.read(data);
String str = new String(data, "GBK");
System.out.println(str);
}
//写入字节流OutputStream
public static void getOutputStream(){
try {
FileOutputStream outputStream = new FileOutputStream("D:/2.txt");
String str = "百度(纳斯达克:BIDU),全球最大的中文搜索引擎、最大的中文网站。百度愿景是:成为最懂用户,并能帮助人们成长的全球顶级高科技公司。 [1]
" +
"“百度”二字,来自于八百年前南宋词人辛弃疾的一句词:众里寻他千百度。这句话描述了词人对理想的执着追求。1999年底,身在美国硅谷的李彦宏看到了中国互联网及中文搜索引擎服务的巨大发展潜力,抱着技术改变世界的梦想,他毅然辞掉硅谷的高薪工作,携搜索引擎专利技术,于 2000年1月1日在中关村创建了百度公司。
" +
"百度拥有数万名研发工程师,这是中国乃至全球最为优秀的技术团队。这支队伍掌握着世界上最为先进的搜索引擎技术,使百度成为中国掌握世界尖端科学核心技术的中国高科技企业,也使中国成为美国、俄罗斯、和韩国之外,全球仅有的4个拥有搜索引擎核心技术的国家之一。
";
outputStream.write(str.getBytes("GBK"));
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//BufferReader和BufferWriter字符流的写入和读取
public static void getBufferReaderAndWriter() throws IOException {
Path path = Paths.get("D:/1.txt");
if (path.toFile().exists()) {
long size = path.toFile().length()/(1024*1024);
System.out.println("文件大小为:" + size + "M");
}
//产生乱码
BufferedReader reader = new BufferedReader(new FileReader(new File("D:/1.txt")));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("D:/3.txt")));
String line = null;
try {
if ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
} catch (Exception e) {
System.out.println(e);
}finally {
writer.close();
}
}
//InputStreamReader和OutputStreamWriter的用法
public static void getOutAndInputStreamReader() throws IOException {
Path path = Paths.get("D:/1.txt");
if (path.toFile().exists()) {
long size = path.toFile().length()/(1024*1024);
System.out.println("文件大小为:" + size + "M");
}
//不产生乱码
// 定义一个指向D:/TEXT.TXT 的字节流、字节流转换成InputStreamReader 、InputStreamReader 转换成带缓存的bufferedReader
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path.toFile()),"GBK"));
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/4.txt"))));
String line = null;
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
bfw.write(line);
bfw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
bfw.close();
}
}
//newBufferedReader和newBufferedWriter的用法
public static void getnewBufferedReaderAndWriter() throws IOException {
Path path = Paths.get("D:/1.txt");
if (path.toFile().exists()) {
System.out.println("文件存在");
}
/**File转化为Path*/
File file = new File("D:/5.txt");
Path path1 = file.toPath();
/**Path转化为File*/
File file1 = path1.toFile();
BufferedReader reader = Files.newBufferedReader(path, Charset.forName("GBK"));
BufferedWriter writer = Files.newBufferedWriter(Paths.get("D:/5.txt"));
String line = null;
try {
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
writer.close();
}
}
/**对象转化为json,json转化为map*/
public static void Convert() throws IOException {
User user = new User("yiqq",28);
System.out.println(user);
//对象转Json
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
//Json转map
Map param = objectMapper.readValue(json, Map.class);
System.out.println(param);
}
}