java读写文件IO
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的更多相关文章
- Java读写文件方法总结
Java读写文件方法总结 Java的读写文件方法在工作中相信有很多的用处的,本人在之前包括现在都在使用Java的读写文件方法来处理数据方面的输入输出,确实很方便.奈何我的记性实在是叫人着急,很多时候既 ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- 【java】java 读写文件
场景:JDK8 将上传的文件,保存到服务器 Java读写文件操作: MultipartFile file InputStream inputStream = file.getInputStream( ...
- java 流 文件 IO
Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...
- 转:Java读写文件各种方法及性能比较
干Java这么久,一直在做WEB相关的项目,一些基础类差不多都已经忘记.经常想得捡起,但总是因为一些原因,不能如愿. 其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来. 文件 ...
- Java读写文件常用方法
一.字符流:读写纯文本(txt,csv等), 1 字符流写文件主要用:FileWriter,BufferedWriter,PrintWriter 1.1 测试 FileWriter 写入 privat ...
- Java 之文件IO编程 之写入
package com.sun; /* * 操作对文件IO的写 * 2014-08-10 */ import java.io.*; public class File_Write { public s ...
- Java 之文件IO编程 之读取
package com.sun; /* * 这里是对文件IO流读取的操作 * 2014-08-10 */ import java.io.*; public class File_test { publ ...
随机推荐
- kubernetes API服务器的安全防护
12.1.了解认证机制 启动API服务器时,通过命令行选项可以开启认证插件. 12.1.1.用户和组 了解用户: 分为两种连接到api服务器的客户端: 1.真实的人 2.pod,使用一种称为Servi ...
- 【科研民工笔记2】Ubuntu 16.04 安装nvidia驱动
我的主机是2060的显卡,用的是安装在U盘中的Ubuntu,开机进入后,因为没有安装驱动,所以界面看以来比较大. 通过手动方式,成功安装驱动,最终成功的方案使用的是run文件安装的方式. 1.手动下载 ...
- 实现ssr服务端渲染demo
最近在研究SSR服务器端渲染,自己写了的小demo. 项目布局 ├── build // 配置文件 │ │── webpack.base // 公共配置 │ │── webpack.clien ...
- MacOS VSCode 安装 GO 插件失败问题解决
0x00 问题重现 Installing golang.org/x/tools/cmd/guru FAILED Installing golang.org/x/tools/cmd/gorename F ...
- CSV Data Set Config 详细使用说明
JMeter 5.1.1 CSV Data Set Config 场景一:线程组中设置:单线程执行1次 如上图所示:变量名称为空时JMeter默认把new 1.txt的文件首行作为变量名 再如:此时A ...
- Java虚拟机日志与参数
虚拟机日志 打印GC日志可以使用参数-XX:+PrintGC /** * -Xmx10m -Xms10m -XX:PretenureSizeThreshold=10485760 * -XX:+Prin ...
- Leetcode solution 124: Binary Tree Maximum Path Sum
Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path ...
- 求平方根算法 Heron’s algorithm
求平方根问题 概述:本文介绍一个古老但是高效的求平方根的算法及其python实现,分析它为什么可以快速求解,并说明它为何就是牛顿迭代法的特例. 问题:求一个正实数的平方根. 给定正实数 \(m\),如 ...
- 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】
利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...
- Python-demo(photo)
import osimport urllib import requests#import wximport time from fake_useragent import UserAgentfrom ...