java文件读写

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ludaze
 */

import java.io.*;
import java.util.ArrayList;
public class ReadAndWrite 
{
    // 创建list存取读出来的数据
    ArrayList<String> S_Info = new ArrayList<String>();

    /**
     * @param args the command line arguments
     */
//    ----------------------------------------------------------------------写入文件------------------------------------------------------
// 参考连接 https://blog.csdn.net/szlg510027010/article/details/88974559?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162363563116780274197725%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162363563116780274197725&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-88974559.pc_search_result_cache&utm_term=FileWriter%E5%AF%BC%E5%85%A5&spm=1018.2226.3001.4187

    public void WriteStringToFile2(String filePath) 
    {
        try
        {
            FileWriter fw = new FileWriter(filePath);
            fw.write("hijk\n");
            fw.write("name");
            fw.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

//    ----------------------------------------------------------------------读取文件--------------------------------------------------------
//    参考链接:https://blog.csdn.net/weixin_30827565/article/details/99909368?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162363699916780262564936%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162363699916780262564936&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-99909368.pc_search_result_cache&utm_term=java+filereader%E8%AF%BB%E5%8F%96%E4%B8%80%E8%A1%8C&spm=1018.2226.3001.4187
    // 以行为单位进行读取
    public void Read(String filePath)
    {
        File file = new File(filePath);
        BufferedReader reader = null;
        try 
        {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null)
            {
                S_Info.add(tempString);// 通过循环将每一行存储到S_Info中
            }
        reader.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        } 
        finally
        {
            if (reader != null) 
            {
                try 
                {
                    reader.close();
                } 
                catch (IOException e1) 
                {
                    e1.printStackTrace();
                }
            }
        }
    }
    
    

    public static void main(String[] args) 
    {
        ReadAndWrite rw = new ReadAndWrite();
        rw.WriteStringToFile2("dou/write.txt");
        rw.Read("dou/write.txt");
        System.out.println(rw.S_Info);
    }   
}

视频演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值