JAVA进行基础的文件IO读写

本文提供了一个使用Java进行文件读写的简单示例,包括如何读取文件内容并将其输出到控制台,以及如何将字符串内容写入指定路径的文件中。
 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 public class TestIO {
 8     
 9     public void testRead(String path){
10         FileInputStream is = null;
11         File file = new File(path);
12         try {
13             is = new FileInputStream(file);
14             byte[] byteBuffer = new byte[is.available()];
15             int read = 0;
16             while((read = is.read(byteBuffer))!= -1){
17                 System.out.write(byteBuffer, 0, read);
18             }
19         } catch (FileNotFoundException e) {
20             e.printStackTrace();
21         } catch (IOException e) {
22             e.printStackTrace();
23         }finally{
24             try {
25                 if(is!=null){
26                     is.close();
27                 }
28             } catch (IOException e) {
29                 e.printStackTrace();
30             }
31         }
32     }
33     
34     public void testWrite(String content, String path){
35         File file = new File(path);
36         try {
37             FileOutputStream os = new FileOutputStream(file);
38             byte[] byteBuffer = content.getBytes();
39             os.write(byteBuffer, 0, byteBuffer.length);
40             os.flush();
41             os.close();
42         } catch (FileNotFoundException e) {
43             e.printStackTrace();
44         } catch (IOException e) {
45             e.printStackTrace();
46         }
47     }
48     
49     public static void main(String[] args) {
50         TestIO io = new TestIO();
51         io.testRead("src/test.txt");
52         io.testWrite("this is a test", "src/test.txt");
53         //io.testRead("src/test.txt");
54     }
55     
56 }

 

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;
public class TestIO {public void testRead(String path){FileInputStream is = null;File file = new File(path);try {is = new FileInputStream(file);byte[] byteBuffer = new byte[is.available()];int read = 0;while((read = is.read(byteBuffer))!= -1){System.out.write(byteBuffer, 0, read);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(is!=null){is.close();}} catch (IOException e) {e.printStackTrace();}}}public void testWrite(String content, String path){File file = new File(path);try {FileOutputStream os = new FileOutputStream(file);byte[] byteBuffer = content.getBytes();os.write(byteBuffer, 0, byteBuffer.length);os.flush();os.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {TestIO io = new TestIO();io.testRead("src/test.txt");io.testWrite("this is a test", "src/test.txt");//io.testRead("src/test.txt");}}

 

转载于:https://www.cnblogs.com/zhaochifan/p/5193629.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值