Java 文本输入主要包含两种方法:FileRead -- 按字符读入,InputSreamReader -- 按行输入。

java 文本输出也包含两种方法:FileWriter 和 OuputStreamWriter,这两种都是按字符输出。

应用代码如下:

 package stream.inout;

 import java.io.*;

 public class FileStream {

     public static void main(String a[]){
String inputfilepath = "E:/in.txt";
/*
in.txt data :
12345
in2.txt data :
11
22
33
44
55
output1.txt data :
tttttrrrrreeeeewwwwwqqqqq
output2.txt data :
tttrrreeewwwqqq
*/ //method no.1 =======================================
try{
int []input1 = new int[5];
File inputfile = new File(inputfilepath);
if(inputfile.isFile() && inputfile.exists()){
//这里的读入是按字符读入
FileReader fileread = new FileReader(inputfile);
int tmp = 0;
int i = 0;
while((tmp = fileread.read()) != -1){
input1[i++] = tmp - '0';
}
fileread.close();
for(i = 0; i < 5; i ++)
System.out.println("input1[ " + i +" ]= " + input1[i]);
}else{
System.out.println("file is not exist");
}
}catch (Exception e){
System.err.println("error happened");
e.printStackTrace();
} //method no.2 =======================================
try{
String []input2 = new String[5];
String input2filepath = "E:/in2.txt";
File inputfile = new File(input2filepath);
if(inputfile.isFile() && inputfile.exists()){
//这里的读入是按行读入
InputStreamReader isr = new InputStreamReader(new FileInputStream(inputfile));
BufferedReader br = new BufferedReader(isr);
int i = 0;
String line = null;
//需注意的是每执行一次br.readLine(),就跳入下一行,故引入一个变量来记录
while((line = br.readLine()) != null){
input2[i++] = line;
}
br.close();
isr.close();
for(i = 0; i < 5; i ++)
System.out.println("input2[ " + i +" ]= " + input2[i]); }else{
System.out.println("file is not exist");
}
}catch (Exception e){
System.err.println("error happened");
e.printStackTrace();
} //output method no.1 ============================
String outputfile1 = "E:/output1.txt";
String outputfile2 = "E:/output2.txt";
try{
String []output = {"ttttt", "rrrrr", "eeeee", "wwwww", "qqqqq"};
FileWriter filewriter = new FileWriter(outputfile1, false);
for(int i = 0; i < 5; i ++){
filewriter.write(output[i]);
}
filewriter.close(); }catch (Exception e){
System.err.println("error happened");
e.printStackTrace();
}
//output method no.2 ============================
try {
String []output1 = {"ttt", "rrr", "eee", "www", "qqq"};
OutputStreamWriter outsw = new OutputStreamWriter(new FileOutputStream(outputfile2));
BufferedWriter bw = new BufferedWriter(outsw); for(int i = 0; i < 5; i ++){
bw.write(output1[i]);
}
bw.flush();
bw.close();
outsw.close();
} catch (Exception e) {
// TODO: handle exception
System.err.println("error happened");
e.printStackTrace();
}
} }

java文本输入输出小结的更多相关文章

  1. java IO 流小结

    java IO 流小结 java流类图结构 流的分类 按方向 输入流 输出流 按类型 字节流 字符流 结论:只要是处理纯文本数据,就优先考虑使用字符流. 除此之外都使用字节流.

  2. 在竞赛ACM Java处理输入输出

    一.Java之ACM注意点 1. 类名称必须采用public class Main方式命名 2. 在有些OJ系统上,即便是输出的末尾多了一个“ ”,程序可能会输出错误,所以在我看来好多OJ系统做的是非 ...

  3. java并发包小结(二)

    接上一篇 java并发包小结(一):http://blog.csdn.net/aalansehaiyang52/article/details/8877579 Future 接口Future 接口允许 ...

  4. Java基本输入输出

    Java基本输入输出 基本输入 基本输出 package com.ahabest.demo; public class Test { public static void main(String[] ...

  5. Java输入输出小结

    无论使用哪一种编程语言,输入输出都是我们首当其冲的,因此简单整理了 一下关于Java输入输出知识点,还有些内容摘自其它博客,忘见谅. 第一部分,让我们看一下Java的输出 public class M ...

  6. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  7. JAVA io 包小结

    IO 无非读写 I --> Reader  O--> Writer 为了方便字符 或者 文本文件的 操作创造出了 字符流 尤其是 缓冲字符输入输出流(BufferedReader,Buff ...

  8. Java I/O 小结

    主要内容: 一.输入流基类:InputStream 和 OutputStream(字节流). Reader 和 Writer(字符流) 二.文件字节流:FileInputStream和FileOutp ...

  9. java基本输入输出练习

    java获取用户的输入分两种,一种是字符的输入,一种是整行的输入,要用到java.io包.对于字符输入来说,使用System.in方法可以输入字符:对于整行的输入,可以使用Scanner类的方法获取整 ...

随机推荐

  1. [论文]CA-Tree: A Hierarchical Structure for Efficient and Scalable Coassociation-Based Cluster Ensembles

    作者:Tsaipei Wang, Member, IEEE 发表:IEEE TRANSACTIONS ON SYSTEMS, MAN, AND CYBERNETICS—PART B: CYBERNET ...

  2. TCP/IP协议详解笔记——ARP协议和RARP协议

    ARP:地址解析协议 对于以太网,数据链路层上是根据48bit的以太网地址来确定目的接口,设备驱动程序从不检查IP数据报中的目的IP地址.ARP协议为IP地址到对应的硬件地址之间提供动态映射. 工作过 ...

  3. Linux内核情景分析之消息队列

    早期的Unix通信只有管道与信号,管道的缺点: 所载送的信息是无格式的字节流,不知道分界线在哪,也没通信规范,另外缺乏控制手段,比如保温优先级,管道机制的大小只有1页,管道很容易写满而读取没有及时,发 ...

  4. python脚本传递参数

    给python程序传递参数 运行python脚本时有时需要执行实传递参数 在linux下: [root@Test ~]# cat /opt/python.py #!/usr/local/bin/pyt ...

  5. android的动态代码

    1,Android代码设置Shape,corners,Gradient  (http://blog.csdn.net/houshunwei/article/details/17392409) int ...

  6. 洛谷—— P1609 最小回文数

    https://www.luogu.org/problemnew/show/1609 题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. ...

  7. Jenkins里邮件插件触发器配置和Send to Developers到底是什么意思(转)

    邮件触发类型介绍(Triggers) By default, the onlytrigger configured is the "Failure" trigger.  To ad ...

  8. 剖析ifstream打开含中文路径名文件失败的原因

    http://blog.csdn.net/yukin_xue/article/details/7543423 最近写程序的时候遇到了使用ifstream打开含中文路径文件时失败的问题,在网上翻了一下, ...

  9. nginx 启动,重启,添加开机启动等相关命令

    nginx -t 测试 配置文件是否正确,同时可以查看配置文件路径 nginx -c /usr/local/nginx/conf/nginx.conf   启动nginx ps -ef|grep ng ...

  10. RTC实时时钟驱动

    RTC(Real-Time Clock)实时时钟为操作系统提供了一个可靠的时间,并且在断电的情况下,RTC实时时钟也可以通过电池供电,一直运行下去. RTC通过STRB/LDRB这两个ARM指令向CP ...