用java实现数组读写_Java学习笔记-文件读写和Json数组

Java文件读写

Java中I/O流对文件的读写有很多种方法,百度后主要看了以下三种

第一种方式:使用FileWriter和FileReader,对文件内容按字符读取,代码如下

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,对文件内容进行整行读取,代码如下

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,这种方法以字节的形式写入文件,读取文件时先读取字节数组,再将字节数组转换为字符串形式,代码如下:

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();

类中的整体代码:

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();

}

运行结果如下:

the first way to write and read

the second way to write and read

the third way to write and read

Java中生成Json数组

示例代码

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);

//格式化Json数组

String jsonStr=jsonContainer.toString();

JsonParser jsonParser=new JsonParser();

JsonObject jsonObject=jsonParser.parse(jsonStr).getAsJsonObject();

Gson gson=new GsonBuilder().setPrettyPrinting().create();

System.out.println(gson.toJson(jsonObject));

}

}

运行结果

{

"category": "nba",

"team": "lakers",

"player": [

{

"name": "kobe",

"height": "198cm",

"weight": "115kg"

},

{

"name": "fisher",

"height": "183cm",

"weight": "85kg"

}

]

}

其中JsonObject生成“{}”,JsonArray生成“[]”,可使用addProperty向“{}”中添加数据,使用add向“[]”中添加数据


版权声明:本文为weixin_39764212原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。