[英]How to read a specific line with special characters from multiple txt files (50 files) in Java
Let's say we have 50 txt files with similar format and we want the 2nd line from each txt file.假设我们有 50 个格式相似的 txt 文件,我们想要每个 txt 文件的第二行。 We want only the special character ie hash to get removed from the end result.
我们只想从最终结果中删除特殊字符,即 hash。 Mentioning the demo content of only 3 files from the 50 files...
提到50个文件中只有3个文件的演示内容……
Note: I have special character hash in the files, which I can't mention here.注意:我在文件中有特殊字符 hash,这里不能提及。 Therefore, mentioning hash txt here.
因此,这里提到hash txt。
1.txt 1.txt
hash hash
hash How to run JavaScript hash 如何运行 JavaScript
hash hash
answer of the question here...can be any number of lines这里问题的答案......可以是任意数量的行
2.txt 2.txt
hash hash
hash How to install EclipseIDE on Windows 10 hash 如何在 Windows 上安装 EclipseIDE 10
hash hash
answer of the question here...can be any number of lines这里问题的答案......可以是任意数量的行
3.txt 3.txt
hash hash
hash NetBeans - How to increase the font size of editor, output and menu hash NetBeans - 如何增加编辑器、output 和菜单的字体大小
hash hash
answer of the question here...can be any number of lines这里问题的答案......可以是任意数量的行
The output I want我要的output
How to run JavaScript如何运行 JavaScript
How to install EclipseIDE on Windows 10 Windows上如何安装EclipseIDE 10
NetBeans - How to increase the font size of editor, output and menu NetBeans - 如何增加编辑器、output 和菜单的字体大小
The code I have tried...我试过的代码...
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException{
File[] files = {new File("1.txt"), new File("2.txt")};
// Fetching all the files
for (File file : files) {
if(file.isFile()) {
BufferedReader inputStream = null;
String line;
try {
inputStream = new BufferedReader(new FileReader(file));
while ((myline = inputStream.readLine()) != null) {
System.out.println(myline);
}
}catch(IOException e) {
System.out.println(e);
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
}
To read the second line of each file:读取每个文件的第二行:
String hash = "...";
for (File file : files) {
if (file.isFile()) {
try (BufferedReader reader =
new BufferedReader(new FileReader(file))) {
// Skip first line.
reader.readLine();
// Read second line.
String line = reader.readLine();
// Remove hash from start of line
line = line.substring(hash.length());
System.out.println(line);
}
}
}
The new Path class can contain more than only a File, for instance a file inside a zip, or some file in the web.新路径 class 可以包含多个文件,例如 zip 中的文件,或 web 中的某个文件。
The class Files has many nice utilities. class 文件有许多不错的实用程序。
Path[] files = {Paths.get("1.txt"), Paths.get("2.txt")};
for (Path file : files) {
try (Stream<String> in = Files.lines(Charset.defaultCharset())) {
in.skip(1) // Skip first line.
.forEach(line -> System.out.println(line.replace("hash", ""));
}
}
try-with-resources closes in every case, here in
. try-with-resources 在每种情况下都会关闭,
in
.
The Stream delivers all lines. Stream 送所有线路。
The following code will work.以下代码将起作用。
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Splitter {
public static void main(String[] args) throws IOException {
File[] files = {new File("1.txt"), new File("2.txt")};
// Fetching all the files
for (File file : files) {
if(file.isFile()) {
BufferedReader inputStream = null;
String line;
try {
inputStream = new BufferedReader(new FileReader(file));
while ((line = inputStream.readLine()) != null) {
line = line.replaceAll("@", "");
System.out.println(line);
}
}catch(IOException e) {
System.out.println(e);
}
finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
}
line = line.replaceAll("@", "");
- replace @
with the special character in your actual file. - 用实际文件中的特殊字符替换
@
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.