Java file read and write
In Java I / O streams there are many ways to read and write files, read the following three major after Baidu
The first way: using the FileReader and FileWriter, reading the contents of the file by character, as follows
String dir = "E:\\soft\\aaa\\a.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建FileWriter对象
FileWriter writer = new FileWriter(file);
//向文件中写入内容
writer.write("the first way to write and read");
writer.flush();
writer.close();
//创建FileReader对象,读取文件中的内容
FileReader reader = new FileReader(file);
char[] ch = new char[100];
reader.read(ch);
for(char c:ch) {
System.out.print(c);
}
System.out.println();
reader.close();
The second way: using packaging BuffredReader BufferedWriter, and, the entire contents of the file to read the line, as follows
String dir = "E:\\soft\\aaa\\b.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建BufferedWriter对象并向文件写入内容
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//向文件中写入内容
bw.write("the second way to write and read");
bw.flush();
bw.close();
//创建BufferedReader读取文件内容
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
The third way: Use a FileOutputStream and FileInputStream, this method writes a file in bytes, the first byte array is read when reading the file, then the byte array into a string, as follows:
String dir = "E:\\soft\\aaa\\c.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建FileOutputStream对象,写入内容
FileOutputStream fos = new FileOutputStream(file);
//向文件中写入内容
fos.write("the third way to write and read".getBytes());
fos.close();
//创建FileInputStream对象,读取文件内容
FileInputStream fis = new FileInputStream(file);
byte[] bys = new byte[100];
while (fis.read(bys, 0, bys.length)!=-1) {
//将字节数组转换为字符串
System.out.println(new String(bys));
}
fis.close();
The whole class codes:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileRW {
//第一种方式:使用FileWriter和FileReader
public void firstWay() throws IOException {
String dir = "E:\\soft\\aaa\\a.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建FileWriter对象
FileWriter writer = new FileWriter(file);
//向文件中写入内容
writer.write("the first way to write and read");
writer.flush();
writer.close();
//创建FileReader对象,读取文件中的内容
FileReader reader = new FileReader(file);
char[] ch = new char[100];
reader.read(ch);
for(char c:ch) {
System.out.print(c);
}
System.out.println();
reader.close();
}
//第二种方式:使用BuffredReader和BufferedWriter
public void secondWay() throws IOException {
String dir = "E:\\soft\\aaa\\b.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建BufferedWriter对象并向文件写入内容
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//向文件中写入内容
bw.write("the second way to write and read");
bw.flush();
bw.close();
//创建BufferedReader读取文件内容
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
}
//第三种方式:使用FileInputStream和FileOutputStream
public void thirdWay() throws IOException {
String dir = "E:\\soft\\aaa\\c.txt";
File file = new File(dir);
//如果文件不存在,创建文件
if (!file.exists())
file.createNewFile();
//创建FileOutputStream对象,写入内容
FileOutputStream fos = new FileOutputStream(file);
//向文件中写入内容
fos.write("the third way to write and read".getBytes());
fos.close();
//创建FileInputStream对象,读取文件内容
FileInputStream fis = new FileInputStream(file);
byte[] bys = new byte[100];
while (fis.read(bys, 0, bys.length)!=-1) {
//将字节数组转换为字符串
System.out.println(new String(bys));
}
fis.close();
}
public static void main(String[] args) throws IOException {
FileRW fileRW = new FileRW();
fileRW.firstWay();
fileRW.secondWay();
fileRW.thirdWay();
}
Results are as follows:
the first way to write and read
the second way to write and read
the third way to write and read
Java generated Json array
Sample Code
import com.google.gson.*;
public class Main{
public static void main(String[] args) {
//创建一个json对象,相当于一个容器,当然这个容器还可以套在另外一个容器里面,这个看业务需要
JsonObject jsonContainer =new JsonObject();
//为当前的json对象添加键值对
jsonContainer.addProperty("category", "nba");
jsonContainer.addProperty("team", "lakers");
//构建json数组,数组里面也是json
JsonArray arrayPlayer = new JsonArray();
//构建json数组中的对象
JsonObject player1 = new JsonObject();
player1.addProperty("name", "kobe");
player1.addProperty("height", "198cm");
player1.addProperty("weight", "115kg");
JsonObject player2 = new JsonObject();
player2.addProperty("name", "fisher");
player2.addProperty("height", "183cm");
player2.addProperty("weight", "85kg");
//将json对象添加到数组中
arrayPlayer.add(player1);
arrayPlayer.add(player2);
//最后将json数组装到jsonContainer中
jsonContainer.add("player", arrayPlayer);
System.out.println(jsonContainer);
}
}
operation result
{"category":"nba","team":"lakers","player":[{"name":"kobe","height":"198cm","weight":"115kg"},{"name":"fisher","height":"183cm","weight":"85kg"}]}
Formatted as:
{
"category": "nba",
"team": "lakers",
"player": [{
"name": "kobe",
"height": "198cm",
"weight": "115kg"
}, {
"name": "fisher",
"height": "183cm",
"weight": "85kg"
}]
}
Wherein JsonObject
generating "{}", JsonArray
to generate "[]" may be used addProperty
to add data to "{}", a add
data is added to "[]" in