IO流

本文提供了一个使用Java进行文件读写的示例代码,包括如何使用FileWriter和BufferedWriter将字符串写入文件,以及如何使用BufferedReader从文件中读取字符串。示例展示了基本的异常处理方法。
package Suchas;
 2  import java.io.*;
 3 
 4 public class FileIO 
 5 {
 6     public FileIO(String str)
 7      {
 8          File f = new File("data.txt");
 9          try 
10          {
11              File file = new File("data.txt");
12              FileWriter fw = new FileWriter(file);
13              BufferedWriter bw = new BufferedWriter(fw);
14             bw.write(str);
15              bw.close();
16          } catch (IOException e)
17          {
18             e.printStackTrace();
19         }
20     }
21     public static void main(String[] args) 
22     {
23          String str = "12345abcdef@#%&*软件工程";
24          new File(str);
25  
26      }
27  
28  }
复制代码
复制代码
 1 package Suchas;
 2  import java.io.BufferedReader;
 3  import java.io.File;
 4  import java.io.FileNotFoundException;
 5  import java.io.FileReader;
 6  import java.io.IOException;
 7  public class Filedemo
 8  {
 9      public Filedemo()
10      {
11          File f = new File("data.txt");
12              try {
13                  BufferedReader br = new BufferedReader(new FileReader(f));
14                 String str = br.readLine();
15                  System.out.println(str);
16                  br.close();
17             } catch (FileNotFoundException e) 
18              {
19                  e.printStackTrace();
20              } catch (IOException e) 
21              {
22                  e.printStackTrace();
23              }    
24      }
25      public static void main(String[] args) 
26      {
27          new Filedemo();
28  
29      }
30  
31  }

转载于:https://www.cnblogs.com/heyishuozaijian/p/11028801.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值