java输入输出流练习代码实例

本文提供了一个Java程序示例,展示了如何使用IO流进行基本的文件读写操作。包括从键盘读取输入、从文件读取内容并拼接、从字符串读取字节以及将字符串写入到文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




import java.io.*;

/*
* @src http://eric-619.iteye.com/blog/693704
* IoStreamTest.java
*/
public class IoStreamTest {

public static void main(String[] args)throws IOException{
IoStreamTest ist = new IoStreamTest();
ist.samples();
}
public void samples() throws IOException {

//1. 这是从键盘读入一行数据,返回的是一个字符串
// BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter a line:");
// System.out.println(stdin.readLine()+"1");
// System.out.println(stdin.readLine()+"2");
// System.out.println(stdin.readLine()+"3");
//2. 这是从文件中逐行读入数据

BufferedReader in = new BufferedReader(new FileReader("D:\\document\\j2ee\\j2se\\j2se note\\j2se_note.txt"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + "\n";
System.out.println(s2);
in.close();


//3. 这是从一个字符串中逐个读入字节
// StringReader in1 = new StringReader(s2);
// int c;
// while((c = in1.read()) != -1)
// System.out.print((char)c);


//4. 这是将一个字符串写入文件
// try {
// BufferedReader in2 = new BufferedReader(new StringReader(s2));
// PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
// int lineCount = 1;
// while((s = in2.readLine()) != null)
// out1.println(lineCount++ + ": " + s);
// out1.close();
// } catch(EOFException e){
// System.err.println("End of stream");
// }
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值