package com.rl.io.output;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*FileReader的构造器
*/
public class IOReaderDemo {
public static void main(String[] args) {
readerFile();
}
public static void readerFile() {
FileReader fr = null;
try {
//创建FileReader对象
fr = new FileReader("helloworld.txt");
/**
//读取单个字符,返回的是字符的ascii码,强转为字符
int num = fr.read();
System.out.println((char)num);
*/
//如果整个文本读取完毕,最后没有字符会返回-1
int num = 0;
//通过循环来读取字符,判断跳出循环的标志是num=-1
while((num = fr.read()) != -1){
//打印字符
System.out.print((char)num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(fr != null){
try {
//colse的异常处理,释放资源
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.rl.io.output;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*读取方式
*/
public class IOReaderDemo1 {
public static void main(String[] args) {
readerFile();
}
public static void readerFile() {
FileReader fr = null;
try {
//创建FileReader对象
fr = new FileReader("helloworld.txt");
//创建一个字符的数组
char[] chs =new char[5];
//读取5个字符放入数组当中,返回的数值是读取到的字符串
int num = fr.read(chs);
System.out.println(num);
String str = new String(chs);
System.out.println(str);
System.out.println("------------");
//读取1个字符放入数组当中,返回的数值是读取到的字符串
num = fr.read(chs);
if(num <5){
str = new String(chs,0,num);
}
System.out.println(num);
System.out.println(str);
//如果num中的数值为-1,表示数组已经读完
char[] chs1 = new char[1024];
int num1=0;
//循环条件的判断边界是fr.read(char[]),如果返回值为-1,说明文件已经读取完毕
while((num1 = fr.read(chs1) ) != -1){
System.out.println(new String(chs1,0,num1));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(fr != null){
try {
//colse的异常处理,释放资源
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.rl.io.copyfile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* 例子:把一个java文件拷贝到项目根目录
* 分析:
*1、创建字符输入流对象
*2、创建字符输出流对象
*3、把输出流输入的数据写入输出流中
*4、关闭资源
*/
public class CopyFileDemo {
public static void main(String[] args) {
copy1();
System.out.println("done");
}
/**
* 拷贝文件方式1
*/
public static void copy(){
Reader reader = null;
Writer writer = null;
try {
//创建文件读取对象
reader = new FileReader("E:/eclipse/eclipse/workspace/io_demo/src/com/rl/io/output/IOReaderDemo.java");
//创建写文件的对象
writer = new FileWriter("IOReaderDemo.java");
int num = -1;
while((num = reader.read()) != -1){
writer.write(num);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//先开谁就后关闭谁
if(writer != null)
writer.close();
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 拷贝文件方式2
*/
public static void copy1(){
Reader reader = null;
Writer writer = null;
try {
//创建文件读取对象
reader = new FileReader("E:/eclipse/eclipse/workspace/io_demo/src/com/rl/io/output/IOReaderDemo.java");
//创建写文件的对象
writer = new FileWriter("IOReaderDemo.java");
//定义每次的读取的长度的对象
int len= -1;
//定义存储读取内容的数组
char[] chs = new char[1024];
//当len !=-1时,一直读取
while((len = reader.read(chs)) != -1){
//把读取的文件写入到目标文件中
writer.write(chs,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
//先开谁就后关闭谁
if(writer != null)
writer.close();
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}