package Common.readFile;

import Common.tool.User;
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: 2018/12/21
* @description:
*/
public class UserIO {
private static final ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) throws IOException {
getFileInputStream();//FileInputStream
getFileOutputStream();//FileOutputStream
getBufferedReaderAndBufferedWriter();//BufferedReader和BufferedWriter
getInputStreamReader();//InputStreamReader
getnewBufferedReader();//Path和newBufferedReader
Convert();//对象转化为json,json转化为map
} /**FileInputStream的用法*/
public static void getFileInputStream(){
try {
String path = "D:/1.txt";
File file = new File(path);
//参数是file或者path,都一样,都可以
FileInputStream fileInputStream = new FileInputStream(path);//文件输入流
byte[] data = new byte[1024];
fileInputStream.read(data);
String str = new String(data, "GBK");
System.out.println(str);
} catch (Exception e) { }
}
/**FileOutputStream的用法*/
public static void getFileOutputStream(){
try {
FileOutputStream fileOutputStream = new FileOutputStream("D:/02.txt");
String name = "FileOutputStream类提供了多种文件写入方法,可以单独写一个字节到文" +
"件,也可以写一个byte数组到文件,也可以取byte数组的部分数据写入到文件。";
fileOutputStream.write(name.getBytes("utf-8"));//文件输出流
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} }
/**BufferedReader和BufferedWriter的用法*/
public static void getBufferedReaderAndBufferedWriter() 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 bfw = new BufferedWriter(new FileWriter(new File("D:/03.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();
}
}
/**InputStreamReader的用法*/
public static void getInputStreamReader() 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 InputStreamReader(new FileInputStream(path.toFile()),"GBK"));
BufferedWriter bfw = new BufferedWriter(new FileWriter(new File("D:/04.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();
}
} /**Path和newBufferedReader的用法*/
public static void getnewBufferedReader() throws IOException {
Path path = Paths.get("D:/1.txt");
if (path.toFile().exists()) {
System.out.println("文件存在");
}
BufferedReader reader = Files.newBufferedReader(path, Charset.forName("GBK"));
BufferedWriter bfw = new BufferedWriter(new FileWriter(new File("D:/05.txt")));
String line = null;
try {
while ((line = reader.readLine()) != null) {
bfw.write(line);
bfw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
bfw.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);
}
}

java读写文件IO的更多相关文章

  1. Java读写文件方法总结

    Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...

  2. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  3. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  4. 【java】java 读写文件

    场景:JDK8  将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...

  5. java 流 文件 IO

    Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...

  6. 转:Java读写文件各种方法及性能比较

    干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...

  7. Java读写文件常用方法

    一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...

  8. Java 之文件IO编程 之写入

    package com.sun; /* * 操作对文件IO的写 * 2014-08-10 */ import java.io.*; public class File_Write { public s ...

  9. Java 之文件IO编程 之读取

    package com.sun; /* * 这里是对文件IO流读取的操作 * 2014-08-10 */ import java.io.*; public class File_test { publ ...

随机推荐

  1. SonarQube系列二、分析dotnet core/C#代码

    [前言] 本系列主要讲述sonarqube的安装部署以及如何集成jenkins自动化分析.netcore项目.目录如下: SonarQube系列一.Linux安装与部署 SonarQube系列二.分析 ...

  2. 防抖(debounce)和节流(throttle)

    场景说明:一般我们在前端页面中会给元素绑定click.scroll.onmousemove.resize等事件,这些事件的执行函数如果是去发请求获取数据的话,我们无意识的连续点击或者连续滚动会给服务器 ...

  3. nginx之gzip压缩提升网站速度

    目录: 为啥使用gzip压缩 nginx使用gzip gzip的常用配置参数 nginx配置gzip 注意 为啥使用gzip压缩 开启nginx的gzip压缩,网页中的js,css等静态资源的大小会大 ...

  4. jmeter之beanshell使用

    beanshell官网:http://www.BeanShell.org/ 一.beanshell介绍 是一种完全符合Java语法规范的轻量级的脚本语言: 相当于一个小巧免费嵌入式的Java源代码解释 ...

  5. Python day02 课堂笔记

    今天是第二天学习Python课程,主要从格式化输出,逻辑运算,编码,数据类型 这几个方面来学习. 1.格式化输出: % : 占位符 %s:字符串 %d:数字 注意: 在格式化的输出中,如果要输出%(因 ...

  6. springboot启动慢解决方法

    jdk的配置文件中,使用securerandom.source设置了熵源: cat /usr/java/jdk1.8.0_121/jre/lib/security/java.security secu ...

  7. n的阶乘尾数有几个0

    /* n!尾数有几个0 */ #include <iostream> using namespace std; void find0(int n); int find(int i,int ...

  8. React引入AntD按需加载报错

    背景:React使用create-react-app脚手架创建,然后yarn run eject暴露了配置之后修改less配置, 需求:实现antd组件按需加载与修改主题. 一开始是按照webpack ...

  9. 原生js实现的一个随机颜色的简单效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...

  10. cogs 80. 石子归并 动态规划

    80. 石子归并 ★★   输入文件:shizi.in   输出文件:shizi.out   简单对比时间限制:1 s   内存限制:128 MB 设有N堆沙(shi)子排成一排,其编号为1,2,3, ...