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. 让你的qstardict读单词

    作为编程行当的人员,英语是躲不掉的,很多资料英文更加有效,字典就显得尤为重要,我希望字典不但能查到中文意思,还能发生,那就跟我来吧: 一.安装字典程序: pacman -S qstartdic sox ...

  2. hdu 4535(排列组合之错排公式)

    吉哥系列故事——礼尚往来 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  3. Codeforces Round #454 C. Shockers【模拟/hash】

    C. Shockers time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  4. 51Nod 1272最大距离 (树状数组维护前缀最小值)

    题目链接 最大距离 其实主流解法应该是单调栈……我用了树状数组. #include <bits/stdc++.h> using namespace std; #define rep(i, ...

  5. bzoj 5125: [Lydsy1712月赛]小Q的书架

    新学了一波 决策单调性 dp 套路.... 这种dp一般是长这样的 => f[i][j] = max/min  { f[i-1][k] + cost(k+1,j)} ,其中cost函数满足四边形 ...

  6. 打包python脚本为exe

    更新pip 安装

  7. Aria2使用教程

    Aria2 是一个轻量级多协议和多源命令行下载实用工具.它支持 HTTP / HTTPS, FTP, SFTP, bt 和 Metalink.通过内置 Aria2 可以操作 json - rpc 和 ...

  8. LAN、WAN、WLAN的区别

    听语音 浏览:22034 | 更新:2015-06-22 20:10 | 标签:网线 1 2 3 4 5 6 7 分步阅读 好多朋友在群内问我路由器如何配置,本来还耐心解答,但是他竟然连LAN.WAN ...

  9. NetBeans菜单栏字体太小了

    NetBeans菜单栏字体太小了,导致很难看 解决方法:在netbeans的快捷方式内加入"netbeans.exe" --fontsize 12参数.还可以通过配置NetBean ...

  10. hdu1862

    //开始把student stu[100000]放置在main()中导致栈溢出,所以必须放在全局位置, //可以调用数组的排序函数sort,包含头文件#include<algorithm> ...