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] Remove Nth Node From End of List 快慢指针

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. duilib入门简明教程 -- 响应按钮事件(4) (转)

    原文转自 http://www.cnblogs.com/Alberl/p/3343610.html     上一个Hello World的教程里有一句代码是这样的:CControlUI *pWnd = ...

  3. 【Visual Studio】设置使用多字符集

  4. JWT在PHP使用及问题处理

    官网 https://jwt.io/ 3.0版本 https://github.com/lcobucci/jwt 安装 composer require lcobucci/jwt 依赖 PHP 5.5 ...

  5. localStorage.getItem

    WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...

  6. 使用OPENROWSET爆破SQL Server密码

    使用OPENROWSET爆破SQL Server密码   OPENROWSET函数是SQL Server提供的一个连接函数.它可以用于使用OLE DB方式连接一个数据库,并进行数据查询等操作.使用该函 ...

  7. hdu 4823 Energy Conversion 构造

    题目链接:HDU - 4823 魔法师百小度也有遇到难题的时候——现在,百小度正在一个古老的石门面前,石门上有一段古老的魔法文字,读懂这种魔法文字需要耗费大量的能量和大量的脑力.过了许久,百小度终于读 ...

  8. html中常用的标签小结

    内容详细标签: <h1>~<h6>标题标签<pre>格式化文本<u>下划线(underline)<i>斜体字(italics)<cit ...

  9. IDEA使用Maven打包时如何去掉测试阶段

    如图

  10. DotnetBrowser高级教程-(4)使用MVC框架2-接收与返回模型

    在上一节,我们搭建了基本的mvc框架,这一节,我们将实现数据的接受与返回,具体操作如下: 1.新建Model目录,新增模型类Person.cs,代码如下: public class Person { ...