文件输入输出操作(字符流)
2015-06-22 15:43
525 查看
package test;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class ArraysDemo{
public static void main(String args[]) throws IOException{
write1();
read1();
write2();
read2();
}
public static void write1() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Writer out=new FileWriter(f);
out.write("hello");
out.close();
}
public static void write2() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Writer out=new FileWriter(f,true);
out.write("\r\nword");//\r\n换行
out.close();
}
public static void read1() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Reader input=new FileReader(f);
char[] c=new char[1024];
int len=input.read(c);
input.close();
System.out.println("read1():\n"+new String(c,0,len));
}
public static void read2() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Reader input=new FileReader(f);
int temp=0;
List<Character> list=new ArrayList<Character>();
while((temp=input.read())!=-1){
list.add((char)temp);
}
char[] b=new char[list.size()];
for(int i=0,j=list.size();i<j;i++){
b[i]=list.get(i);
}
input.close();
System.out.println("read2():\n"+new String(b));
}
};
/**
read1():
hello
read2():
hello
hello
**/
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class ArraysDemo{
public static void main(String args[]) throws IOException{
write1();
read1();
write2();
read2();
}
public static void write1() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Writer out=new FileWriter(f);
out.write("hello");
out.close();
}
public static void write2() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Writer out=new FileWriter(f,true);
out.write("\r\nword");//\r\n换行
out.close();
}
public static void read1() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Reader input=new FileReader(f);
char[] c=new char[1024];
int len=input.read(c);
input.close();
System.out.println("read1():\n"+new String(c,0,len));
}
public static void read2() throws IOException{
File f = new File("d:"+File.separator+"text.txt") ; // 实例化File类的对象
Reader input=new FileReader(f);
int temp=0;
List<Character> list=new ArrayList<Character>();
while((temp=input.read())!=-1){
list.add((char)temp);
}
char[] b=new char[list.size()];
for(int i=0,j=list.size();i<j;i++){
b[i]=list.get(i);
}
input.close();
System.out.println("read2():\n"+new String(b));
}
};
/**
read1():
hello
read2():
hello
hello
**/
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统