import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Notepad {
private static int line = 0; //行号
public static void main(String[] args) {
String filePath = "D://小说//第一集.txt";// 文件路径
File file = new File(filePath);
FileReader fr = null;
int lineNum = 50; // 每行多少个字
try {
fr = new FileReader(file);
fr.read();
BufferedReader read = new BufferedReader(fr);
while (read.read() != -1) {
String as = read.readLine();
readByLineNumber(as,lineNum);
}
} catch (FileNotFoundException e) {
System.out.print("文件没有找到,请输入正确的文件路径");
} catch (IOException e) {
}
}
/**
* 自动换行读取
* @param as 读取的字符
* @param lineNum 每行多少个字
*/
private static void readByLineNumber(String as,int lineNum){
for (int i = 0; i < as.length();) {
line++;
if(as.length()>=i+lineNum){
System.out.println(as.substring(i, i + lineNum)+ "----------" + line);
as=as.substring(i+lineNum,as.length());
}else{
System.out.println(as+ "----------" + line);
return;
}
}
}
}
本文介绍了一个使用Java编程语言实现的功能,即从指定路径的文本文件中读取内容,并自动换行显示每行不超过指定数量的文字。通过创建`File`对象来获取文件,使用`FileReader`和`BufferedReader`进行读取操作,实现了对文本内容的高效处理和展示。
1160

被折叠的 条评论
为什么被折叠?



