如何用java实现读取txt文件并对其内容进行处理?java菜菜鸟,求好心人来个完整的程序。

如题所述

在Java中读取和处理txt文件,有多种方式可以选择。例如,可以以字节为单位读取文件,这通常用于读取二进制文件,如图片、声音、影像等。下面是具体实现代码:

public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
showAvailableBytes(in);
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}}

另一种方法是以字符为单位读取文件,常用于读取文本、数字等类型的文件。以下是具体实现代码:

public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length) && (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}}
}}
} catch (Exception e1) {
e1.printStackTrace();
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}}
}

此外,还可以以行为单位读取文件,常用于读取面向行的格式化文件。具体实现代码如下:

public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
while ((tempString = reader.readLine()) != null) {
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}}
}

如果需要随机读取文件内容,可以使用RandomAccessFile类。具体实现代码如下:

public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
randomFile = new RandomAccessFile(fileName, "r");
long fileLength = randomFile.length();
int beginIndex = (fileLength > 4) ? 4 : 0;
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}}
}

此外,还可以将内容追加到文件尾部。使用RandomAccessFile和FileWriter类可以实现这一功能。具体实现代码如下:

public static void appendMethodA(String fileName, String content) {
try {
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}

}

public static void appendMethodB(String fileName, String content) {
try {
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}

}

最后,可以通过调用readFileByLines方法来显示追加后的文件内容。
温馨提示:答案为网友推荐,仅供参考