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. [LeetCode] String to Integer (atoi) 字符串

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. poj 2100(尺取法)

    Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 ...

  3. Java 获取指定日期的方法总结

    原文地址:http://bdcwl.blog.163.com/blog/static/765222652009104171521/ SimpleDateFormat sdf = new SimpleD ...

  4. 迷宫问题(DFS,BFS)

    /******************************** 啊哈!算法 深度优先搜索算法 迷宫问题 输入: 5 4 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 ...

  5. luogu P2744 [USACO5.3]量取牛奶Milk Measuring

    题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...

  6. Mac 安装brew和安装composer

    一.安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/mast ...

  7. Linux命令大总结

    from http://elain.blog.51cto.com/3339379/623310 Linux命令大总结------------------------------------------ ...

  8. delphi 按位运算 not and or xor shl shr

    delphi 按位运算 not and or xor shl shr unit Unit1;   interface   uses   Windows, Messages, SysUtils, Var ...

  9. ci框架文件上传

    控制器类代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Upload ex ...

  10. 更改VS2010 工程名的方法

    哇~~~~~~~啦啦啦~~~~~~~~ 太开心了,通过不断的尝试,我终于知道怎么更改VS2010的工程名了!!! 下面分享给大家: 1.打开自己想要更改名字的工程,用ctrl+h在整个项目中把想更改的 ...