常用的IO代码
来源:互联网 发布:壁纸软件哪个好 编辑:程序博客网 时间:2024/05/18 01:07
以行读入文件:
从键盘读入:
从一个String对象中读取数据
从内存取出格式化输入
输出到文件
BufferedReader in =
new BufferedReader(
new FileReader("F: epalonTestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + " ";
in.close();
new BufferedReader(
new FileReader("F: epalonTestIO.java"));
String s, s2 = new String();
while((s = in.readLine()) != null)
s2 += s + " ";
in.close();
从键盘读入:
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());
new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());
从一个String对象中读取数据
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();
int c;
while((c = in2.read()) != -1)
System.out.println((char)c);
in2.close();
从内存取出格式化输入
try{
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}
catch(EOFException e){
System.out.println("End of stream");
}
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.println((char)in3.readByte());
}
catch(EOFException e){
System.out.println("End of stream");
}
输出到文件
try{
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("F: epalon TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
catch(EOFException ex){
System.out.println("End of stream");
}
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("F: epalon TestIO.out")));
int lineCount = 1;
while((s = in4.readLine()) != null)
out1.println(lineCount++ + ":" + s);
out1.close();
in4.close();
}
catch(EOFException ex){
System.out.println("End of stream");
}
- 常用的IO代码
- IO流常用方法,流的用法,代码例子
- IO流常用方法,流的用法,代码例子
- Java文件读写(IO)常用代码
- IO流-常用的IO流总结
- 常用的IO口电平
- IO中常用的方法
- 常用的IO输入输出流
- java.io的详细代码
- IO-Polling的代码分析
- java IO与网络的常用操作
- java.io.file 的常用文件操作
- Linux 常用io函数的比较
- <细节>C++的常用IO格式功能
- JAVA IO处理文件的常用方法
- java 中常用的io流总结
- IO流的分类及常用案例
- java io流常用类的使用
- JasperReports的一些小技巧
- 第一篇
- JSP/Servlet写验证码
- VC++里面 用ADO访问Oracle数据库[求助,特急]
- Java代码优化TIPS
- 常用的IO代码
- 曾深爱过的人
- 关于蜂产品保健的一些介绍
- 在Java中实现浮点数的精确计算
- 手动创建一个Oracle数据库
- 《CSDN社区电子杂志——测试员杂志》2005年6月第2期,总第6期发布!!
- 菜鸟请教一个愚笨的问题
- 电子书下载地址
- 绑定txt文件到DataGrid