JAVA中IO流读取和写入样例

import java.io.*;

/**
 * 读取txt文件
 * 一次读一行
 * @author ylq
 *
 */
public class oput {


    public static void path( String input,String output) throws IOException {
        //文件路径
        //String filePath="F:\\9ebang\\data\\gis\\output\\1xian50.txt";
        String filePath=input;
        File file=new File(filePath);

        BufferedReader reader = null;//字符输入流进行读取操作读取

        String tempString = null;//每一行的内容

        int line =1;//行号
        FileWriter fw = new FileWriter(output);
        try {
            //输入字节流,FileInputStream主要用来操作文件输入流
            FileInputStream intput = new FileInputStream(file);
            reader = new BufferedReader(new InputStreamReader(intput));

            while ((tempString = reader.readLine()) != null) {
                String[] split = tempString.split("\t");
                System.out.println(split.length);
                if (split.length==3){
                    System.out.println("三个"+split[0]+","+split[1]+","+split[2]);
                    fw.write("["+"\""+split[0]+"\""+","+"\""+split[1]+"\""+","+"\""+split[2]+"\""+"]"+","+"\n");
                }else {
                    fw.write("["+"\""+split[0]+"\""+","+"\""+split[1]+"\""+","+"\""+split[2]+"\""+","+"\""+split[3]+"\""+"]"+","+"\n");
                }


            }
            fw.flush();
            fw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) throws Exception {
        //1号线
        String input1="F:\\9ebang\\data\\gis\\output\\1xian50.txt";
        String output1="F:\\9ebang\\data\\gis\\output\\1xian50apk.txt";
        //5号线
        String input5="F:\\9ebang\\data\\gis\\output\\5xian50.txt";
        String output5="F:\\9ebang\\data\\gis\\output\\5xian50apk.txt";
         path(input5,output5);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值