/*
* Created on 2006-7-18, ReadFile.java
*/
package ite.com.stream.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 读取文件中的字符
*
* @author 覃忠君
* @version 1.00, 2006-7-18
*
* @copyright 长沙亿泰科技有限公司 2006~2007
*/
public class ReadFile {
public static void main(String[] args) {
ReadFile rf = new ReadFile();
rf.operationFile();
rf.writerFileData();
rf.readFileData();
}
public void operationFile() {
File file = new File("file.txt");
// 检查File.txt是否存在
if (file.exists()) {
// 删除file.txt档
file.delete();
} else {
// 在目前的目录下建立一个名为file.txt的文字档
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void writerFileData() {
FileWriter fw = null;
try {
fw = new FileWriter("file.txt");
// 将字串写入文件
fw.write("大家好! /n");
fw.write("祝大家学习JAVA愉快!/n");
fw.write("希望大家能够深入了解JAVA技术!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fw) {
try {
fw.close();
fw = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void readFileData() {
String filename = "file.txt";
FileReader fr = null;
try {
fr = new FileReader(filename);
int c = fr.read();
// 判断是否已读到文件的结尾
while (c != -1) {
// 输出读取到的数据
System.out.print((char) c);
// 从文件中读取数据
c = fr.read();
// 判断是否为断行字节
if (c == 13) {
System.out.println("*****断行字节*****");
// 略过一个字节
fr.skip(1);
// 读取一个字节
c = fr.read();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fr) {
try {
fr.close();
fr = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void getDirectoryAllFile(){
File file = new File("D://workspace//XMLDemo//XML");
File[] list = file.listFiles();
for (int i = 0; i < list.length; i++) {
//判断它是否是文件
if (list[i].isFile()){
System.out.println9(list[i].getName());
}
}
}
}
* Created on 2006-7-18, ReadFile.java
*/
package ite.com.stream.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 读取文件中的字符
*
* @author 覃忠君
* @version 1.00, 2006-7-18
*
* @copyright 长沙亿泰科技有限公司 2006~2007
*/
public class ReadFile {
public static void main(String[] args) {
ReadFile rf = new ReadFile();
rf.operationFile();
rf.writerFileData();
rf.readFileData();
}
public void operationFile() {
File file = new File("file.txt");
// 检查File.txt是否存在
if (file.exists()) {
// 删除file.txt档
file.delete();
} else {
// 在目前的目录下建立一个名为file.txt的文字档
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void writerFileData() {
FileWriter fw = null;
try {
fw = new FileWriter("file.txt");
// 将字串写入文件
fw.write("大家好! /n");
fw.write("祝大家学习JAVA愉快!/n");
fw.write("希望大家能够深入了解JAVA技术!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fw) {
try {
fw.close();
fw = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void readFileData() {
String filename = "file.txt";
FileReader fr = null;
try {
fr = new FileReader(filename);
int c = fr.read();
// 判断是否已读到文件的结尾
while (c != -1) {
// 输出读取到的数据
System.out.print((char) c);
// 从文件中读取数据
c = fr.read();
// 判断是否为断行字节
if (c == 13) {
System.out.println("*****断行字节*****");
// 略过一个字节
fr.skip(1);
// 读取一个字节
c = fr.read();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != fr) {
try {
fr.close();
fr = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void getDirectoryAllFile(){
File file = new File("D://workspace//XMLDemo//XML");
File[] list = file.listFiles();
for (int i = 0; i < list.length; i++) {
//判断它是否是文件
if (list[i].isFile()){
System.out.println9(list[i].getName());
}
}
}
}