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 ...
随机推荐
- Go中的函数和闭包
函数参数和返回值的写法 如果有多个参数是同一个类型,可以简略写: func testReturnFunc(v1,v2 int)(int,int) { x1 := 2 * v1 x2 := 3 * v2 ...
- Web前端开发工程师课程大纲
PHP程序员雷雪松整理出来的一套独一无二的Web前端开发课程.本套Web前端开发课程专门为想励志成为优秀web前端工程师的学习者而总结归纳的,本套Web前端课程舍弃了一些不常用的即将废弃的HTML标签 ...
- [转载]MongoDB管理基础
1. 启动和停止MongoDB: 执行mongod命令启动MongoDB服务器.mongod有很多可配置的选项,我们通过mongod --help可以查看所有选项,这里仅介绍一些主要选项: - ...
- Liunx查看后1000行的命令以及查看中间部分
linux 如何显示一个文件的某几行(中间几行) [一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1 ...
- Spring 集成Kafka(完整版)
前面的文章我们已经完成了Kafka基于Zookeeper的集群的搭建了.Kafka集群搭建请点我.记过几天的研究已经实现Spring的集成了.本文重点 jar包准备 集成是基于spring-integ ...
- 为什么双重检查锁模式需要 volatile ?
双重检查锁定(Double check locked)模式经常会出现在一些框架源码中,目的是为了延迟初始化变量.这个模式还可以用来创建单例.下面来看一个 Spring 中双重检查锁定的例子. 这个例子 ...
- java优雅注释原则和代码格式列举
一.java的三种注释类型 单行注释:// ...... 块注释:/* ...... */ 文档注释:/** ...... */ 二.指导原则 注释不能美化糟糕的代码,碰到糟糕的代码就重新写吧. 用代 ...
- C#.Net实现AutoCAD块属性提取
https://blog.csdn.net/dengyiyu/article/details/2201175 本文主要给大家介绍一下SmartSoft中用C#.Net实现AutoCAD块属性提取的方法 ...
- 入门MySQL——DML语句篇
前言: 在上篇文章中,主要为大家介绍的是DDL语句的用法,可能细心的同学已经发现了.本篇文章将主要聚焦于DML语句,为大家讲解表数据相关操作. 这里说明下DDL与DML语句的分类,可能有的同学还不太 ...
- 测试常用的sql语句
1.查询:select * from table_name where 条件语句; SELECT * from sms_runwater WHERE message LIKE "%自有支付% ...