java怎样从指定行号开始读取文txt文件

java怎样从指定行号开始读取文txt文件,第1张

public class LineNumberReader extends BufferedReader

跟踪行号的缓冲字符输入流。此类定义了方法 setLineNumber(int) 和 getLineNumber(),它们可分别用于设置和获取当前行号。

默认情况下,行编号从 0 开始。该行号随数据读取在每个行结束符处递增,并且可以通过调用 setLineNumber(int) 更改行号。但要注意的是,setLineNumber(int) 不会实际更改流中的当前位置;它只更改将由 getLineNumber() 返回的值。

可认为行在遇到以下符号之一时结束:换行符('\n')、回车符('\r')、回车后紧跟换行符。

java读取文件方法大全

一、多种方式读文件内容

1、按字节读取文件内容

2、按字符读取文件内容

3、按行读取文件内容

4、随机读取文件内容

Java代码

1 import javaioBufferedReader;

2 import javaioFile;

3 import javaioFileInputStream;

4 import javaioFileReader;

5 import javaioIOException;

6 import javaioInputStream;

7 import javaioInputStreamReader;

8 import javaioRandomAccessFile;

9 import javaioReader;

10

11 public class ReadFromFile {

12 /

13 以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。

14

15 @param fileName

16 文件的名

17 /

18 public static void readFileByBytes(String fileName) {

19 File file = new File(fileName);

20 InputStream in = null;

21 try {

22 Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");

23 // 一次读一个字节

24 in = new FileInputStream(file);

25 int tempbyte;

26 while ((tempbyte = inread()) != -1) {

27 Systemoutwrite(tempbyte);

28 }

29 inclose();

30 } catch (IOException e) {

31 eprintStackTrace();

32 return;

33 }

34 try {

35 Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");

36 // 一次读多个字节

37 byte[] tempbytes = new byte[100];

38 int byteread = 0;

39 in = new FileInputStream(fileName);

40 ReadFromFileshowAvailableBytes(in);

41 // 读入多个字节到字节数组中,byteread为一次读入的字节数

42 while ((byteread = inread(tempbytes)) != -1) {

43 Systemoutwrite(tempbytes, 0, byteread);

44 }

45 } catch (Exception e1) {

46 e1printStackTrace();

47 } finally {

48 if (in != null) {

49 try {

50 inclose();

51 } catch (IOException e1) {

52 }

53 }

54 }

55 }

56

57 /

58 以字符为单位读取文件,常用于读文本,数字等类型的文件

59

60 @param fileName

61 文件名

62 /

63 public static void readFileByChars(String fileName) {

64 File file = new File(fileName);

65 Reader reader = null;

66 try {

67 Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");

68 // 一次读一个字符

69 reader = new InputStreamReader(new FileInputStream(file));

70 int tempchar;

71 while ((tempchar = readerread()) != -1) {

72 // 对于windows下,\r\n这两个字符在一起时,表示一个换行。

73 // 但如果这两个字符分开显示时,会换两次行。

74 // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

75 if (((char) tempchar) != '\r') {

76 Systemoutprint((char) tempchar);

77 }

78 }

79 readerclose();

80 } catch (Exception e) {

81 eprintStackTrace();

82 }

83 try {

84 Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");

85 // 一次读多个字符

86 char[] tempchars = new char[30];

87 int charread = 0;

88 reader = new InputStreamReader(new FileInputStream(fileName));

89 // 读入多个字符到字符数组中,charread为一次读取字符数

90 while ((charread = readerread(tempchars)) != -1) {

91 // 同样屏蔽掉\r不显示

92 if ((charread == tempcharslength)

93 && (tempchars[tempcharslength - 1] != '\r')) {

94 Systemoutprint(tempchars);

95 } else {

96 for (int i = 0; i < charread; i++) {

97 if (tempchars[i] == '\r') {

98 continue;

99 } else {

100 Systemoutprint(tempchars[i]);

101 }

102 }

103 }

104 }

105

106 } catch (Exception e1) {

107 e1printStackTrace();

108 } finally {

109 if (reader != null) {

110 try {

111 readerclose();

112 } catch (IOException e1) {

113 }

114 }

115 }

116 }

117

118 /

119 以行为单位读取文件,常用于读面向行的格式化文件

120

121 @param fileName

122 文件名

123 /

124 public static void readFileByLines(String fileName) {

125 File file = new File(fileName);

126 BufferedReader reader = null;

127 try {

128 Systemoutprintln("以行为单位读取文件内容,一次读一整行:");

129 reader = new BufferedReader(new FileReader(file));

130 String tempString = null;

131 int line = 1;

132 // 一次读入一行,直到读入null为文件结束

133 while ((tempString = readerreadLine()) != null) {

134 // 显示行号

135 Systemoutprintln("line " + line + ": " + tempString);

136 line++;

137 }

138 readerclose();

139 } catch (IOException e) {

140 eprintStackTrace();

141 } finally {

142 if (reader != null) {

143 try {

144 readerclose();

145 } catch (IOException e1) {

146 }

147 }

148 }

149 }

150

151 /

152 随机读取文件内容

153

154 @param fileName

155 文件名

156 /

157 public static void readFileByRandomAccess(String fileName) {

158 RandomAccessFile randomFile = null;

159 try {

160 Systemoutprintln("随机读取一段文件内容:");

161 // 打开一个随机访问文件流,按只读方式

162 randomFile = new RandomAccessFile(fileName, "r");

163 // 文件长度,字节数

164 long fileLength = randomFilelength();

165 // 读文件的起始位置

166 int beginIndex = (fileLength > 4) 4 : 0;

167 // 将读文件的开始位置移到beginIndex位置。

168 randomFileseek(beginIndex);

169 byte[] bytes = new byte[10];

170 int byteread = 0;

171 // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

172 // 将一次读取的字节数赋给byteread

173 while ((byteread = randomFileread(bytes)) != -1) {

174 Systemoutwrite(bytes, 0, byteread);

175 }

176 } catch (IOException e) {

177 eprintStackTrace();

178 } finally {

179 if (randomFile != null) {

180 try {

181 randomFileclose();

182 } catch (IOException e1) {

183 }

184 }

185 }

186 }

187

188 /

189 显示输入流中还剩的字节数

190

191 @param in

192 /

193 private static void showAvailableBytes(InputStream in) {

194 try {

195 Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());

196 } catch (IOException e) {

197 eprintStackTrace();

198 }

199 }

200

201 public static void main(String[] args) {

202 String fileName = "C:/temp/newTemptxt";

203 ReadFromFilereadFileByBytes(fileName);

204 ReadFromFilereadFileByChars(fileName);

205 ReadFromFilereadFileByLines(fileName);

206 ReadFromFilereadFileByRandomAccess(fileName);

207 }

208 }

二、将内容追加到文件尾部

1 import javaioFileWriter;

2 import javaioIOException;

3 import javaioRandomAccessFile;

4

5 /

6 将内容追加到文件尾部

7 /

8 public class AppendToFile {

9

10 /

11 A方法追加文件:使用RandomAccessFile

12 @param fileName 文件名

13 @param content 追加的内容

14 /

15 public static void appendMethodA(String fileName, String content) {

16 try {

17 // 打开一个随机访问文件流,按读写方式

18 RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");

19 // 文件长度,字节数

20 long fileLength = randomFilelength();

21 //将写文件指针移到文件尾。

22 randomFileseek(fileLength);

23 randomFilewriteBytes(content);

24 randomFileclose();

25 } catch (IOException e) {

26 eprintStackTrace();

27 }

28 }

29

30 /

31 B方法追加文件:使用FileWriter

32 @param fileName

33 @param content

34 /

35 public static void appendMethodB(String fileName, String content) {

36 try {

37 //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件

38 FileWriter writer = new FileWriter(fileName, true);

39 writerwrite(content);

40 writerclose();

41 } catch (IOException e) {

42 eprintStackTrace();

43 }

44 }

45

46 public static void main(String[] args) {

47 String fileName = "C:/temp/newTemptxt";

48 String content = "new append!";

49 //按方法A追加文件

50 AppendToFileappendMethodA(fileName, content);

51 AppendToFileappendMethodA(fileName, "append end \n");

52 //显示文件内容

53 ReadFromFilereadFileByLines(fileName);

54 //按方法B追加文件

55 AppendToFileappendMethodB(fileName, content);

56 AppendToFileappendMethodB(fileName, "append end \n");

57 //显示文件内容

58 ReadFromFilereadFileByLines(fileName);

59 }

60 }

JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代码,需要的可以直接复制使用

public class ReadFromFile {

/

以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。

/

public static void readFileByBytes(String fileName) {

File file = new File(fileName);

InputStream in = null;

try {

Systemoutprintln("以字节为单位读取文件内容,一次读一个字节:");

// 一次读一个字节

in = new FileInputStream(file);

int tempbyte;

while ((tempbyte = inread()) != -1) {

Systemoutwrite(tempbyte);

}

inclose();

} catch (IOException e) {

eprintStackTrace();

return;

}

try {

Systemoutprintln("以字节为单位读取文件内容,一次读多个字节:");

// 一次读多个字节

byte[] tempbytes = new byte[100];

int byteread = 0;

in = new FileInputStream(fileName);

ReadFromFileshowAvailableBytes(in);

// 读入多个字节到字节数组中,byteread为一次读入的字节数

while ((byteread = inread(tempbytes)) != -1) {

Systemoutwrite(tempbytes, 0, byteread);

}

} catch (Exception e1) {

e1printStackTrace();

} finally {

if (in != null) {

try {

inclose();

} catch (IOException e1) {

}

}

}

}

/

以字符为单位读取文件,常用于读文本,数字等类型的文件

/

public static void readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

try {

Systemoutprintln("以字符为单位读取文件内容,一次读一个字节:");

// 一次读一个字符

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = readerread()) != -1) {

// 对于windows下,\r\n这两个字符在一起时,表示一个换行。

// 但如果这两个字符分开显示时,会换两次行。

// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

if (((char) tempchar) != '\r') {

Systemoutprint((char) tempchar);

}

}

readerclose();

} catch (Exception e) {

eprintStackTrace();

}

try {

Systemoutprintln("以字符为单位读取文件内容,一次读多个字节:");

// 一次读多个字符

char[] tempchars = new char[30];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileName));

// 读入多个字符到字符数组中,charread为一次读取字符数

while ((charread = readerread(tempchars)) != -1) {

// 同样屏蔽掉\r不显示

if ((charread == tempcharslength)

&& (tempchars[tempcharslength - 1] != '\r')) {

Systemoutprint(tempchars);

} else {

for (int i = 0; i < charread; i++) {

if (tempchars[i] == '\r') {

continue;

} else {

Systemoutprint(tempchars[i]);

}

}

}

}

} catch (Exception e1) {

e1printStackTrace();

} finally {

if (reader != null) {

try {

readerclose();

} catch (IOException e1) {

}

}

}

}

/

以行为单位读取文件,常用于读面向行的格式化文件

/

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

Systemoutprintln("以行为单位读取文件内容,一次读一整行:");

reader = new BufferedReader(new FileReader(file));

String tempString = null;

int line = 1;

// 一次读入一行,直到读入null为文件结束

while ((tempString = readerreadLine()) != null) {

// 显示行号

Systemoutprintln("line " + line + ": " + tempString);

line++;

}

readerclose();

} catch (IOException e) {

eprintStackTrace();

} finally {

if (reader != null) {

try {

readerclose();

} catch (IOException e1) {

}

}

}

}

/

随机读取文件内容

/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

Systemoutprintln("随机读取一段文件内容:");

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFilelength();

// 读文件的起始位置

int beginIndex = (fileLength > 4) 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFileseek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

while ((byteread = randomFileread(bytes)) != -1) {

Systemoutwrite(bytes, 0, byteread);

}

} catch (IOException e) {

eprintStackTrace();

} finally {

if (randomFile != null) {

try {

randomFileclose();

} catch (IOException e1) {

}

}

}

}

/

显示输入流中还剩的字节数

/

private static void showAvailableBytes(InputStream in) {

try {

Systemoutprintln("当前字节输入流中的字节数为:" + inavailable());

} catch (IOException e) {

eprintStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemptxt";

ReadFromFilereadFileByBytes(fileName);

ReadFromFilereadFileByChars(fileName);

ReadFromFilereadFileByLines(fileName);

ReadFromFilereadFileByRandomAccess(fileName);

}

}

以上就是关于java怎样从指定行号开始读取文txt文件全部的内容,包括:java怎样从指定行号开始读取文txt文件、java文件如何读取、JAVA中读取文件(二进制,字符)内容的几种方等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9507247.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存