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 ...
随机推荐
- (数据科学学习手札66)在ubuntu服务器上部署shiny
一.简介 shiny是R中专门用于开发轻量级web应用的框架,在本地写一个shiny应用并调用非常方便,但如果你希望你的shiny应用能够以远程的方式提供给更多人来使用,就需要将写好的shiny应用部 ...
- 邻域保持嵌入(NPE)
传统的线性降维方法,如主成分分析(PCA).因子分析(FA)等,关注的是样本的方差,能学习线性流形的结构,却无法学习非线性流形.而经典的流形学习方法虽然能够学习非线性流形结构,但由于本身属于直推学习, ...
- java并发编程(四)----(JUC)Lock锁初探
首先我们来回忆一下上一节讲过的synchronized关键字,该关键字用于给代码段或方法加锁,使得某一时刻它修饰的方法或代码段只能被一个线程访问.那么试想,当我们遇到这样的情况:当synchroniz ...
- c语言ld returned 1 exit status😂
在复习c语言过程中遇到, 问题:reverseLinkedList.exe: Permission denied collect2.exe: error: ld returned 1 exit sta ...
- 从零开始学习GDI+ (一)
前言: GDI+从Windows XP操作系统(大概2002-2003年)开始引入的,现在都9102年了,再学习这么古老的技术肯定是过时了.windows桌面程序没落了,随着移动的兴起,用户被惯坏了, ...
- Go开发中的十大常见陷阱[译]
原文: The Top 10 Most Common Mistakes I've Seen in Go Projects 作者: Teiva Harsanyi 译者: Simon Ma 我在Go开发中 ...
- npm命令无响应
npm命令完全无反应,不是加载的那种状态 而是下标不停地在哪里闪... 之后找解决方案,说要删除npmrc文件. 强调:不是nodejs安装目录npm模块下的那个npmrc文件 而是在C:\Users ...
- Sqlserver 游标的写法记录
---游标更新删除当前数据 ---1.声明游标 declare orderNum_03_cursor cursor scroll for select OrderId ,userId from big ...
- Mysql优化(出自官方文档) - 第八篇(索引优化系列)
目录 Mysql优化(出自官方文档) - 第八篇(索引优化系列) Optimization and Indexes 1 Foreign Key Optimization 2 Column Indexe ...
- 解决多字段联合逻辑校验问题【享学Spring MVC】
每篇一句 不要像祥林嫂一样,天天抱怨着生活,日日思考着辞职.得罪点说一句:"沦落"到要跟这样的人共事工作,难道自己身上就没有原因? 前言 本以为洋洋洒洒的把Java/Spring数 ...