java读取txt文件json对象

CodeName.txt
文件中是
{
“English”:“英国”,
”China“:“中国”
}

package com.example.demo.controller;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileTest {

    public static void main(String[] args) throws IOException {
        String dirname = "D:/test/CodeToName.txt";
        List<Country> countries = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        //创建一个文件对象
        File codeNameFile = new File(dirname);
        //从硬盘存在的一个文件,读取其内容到程序中,用FileInputStream
        FileInputStream fileInputStream = new FileInputStream(codeNameFile);
        //把字节流转换成字符流
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        //将字符流读到缓存中,到缓存满了或者你flush的时候,再读入内存,为了提供读的效率而设计的
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String s = null;
        while ((s = bufferedReader.readLine()) != null) {//使用readLine方法,一次读一行
            if ("{".equals(s) || "}".equals(s)) {
                continue;
            }
            String s1 = s.replace("\"", "");
            String s2 = s1.replace(",", "");
            String[] split = s2.split(":");
            Country country = new Country();
            country.setKeyName(split[0].trim());
            country.setName(split[1].trim());
            countries.add(country);
        }
        System.out.println(countries);
    }
}

public class Country {

    private String keyName;
    private String name;

    public String getKeyName() {
        return keyName;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值