如何从文件中读取然后分析这些数据?
How to read from a file and then analyze this data?
我是 Java 编程的初学者(最近开始学习),我需要帮助。
我必须从一个包含数字的文件中读取。我想制作一种从文件中读取的方法。然后我需要分析这些数据并将其写入另一个文件。
我苦恼的是,如果我创建一个方法只是为了从文件中读取数据,还是我还必须将读取的数据保存到一个变量中。这个变量应该在方法内部的什么地方声明(如果在内部,我如何在外部使用它),如果在外部,我如何在方法内部和外部使用它。谁能帮我澄清一下?我究竟做错了什么?
到目前为止我写的代码。我必须阅读的文件有成百上千的数字。
public class Test1 {
public static void main(String[] args) {
String nameFile = "numbers.txt";
File file = new File(nameFile);
String contentFile ="";
}
//Method for reading a .txt file
private static String readFromFile(String nameFile, String contentFile) {
String line = "";
try {
BufferedReader read = new BufferedReader(new FileReader(nameFile));
while((line = read.readLine()) != null) {
line = contentFIle;
}
read.close();
} catch (IOException e) {
System.out.println("There was an error reading from a file");
}
return line;
}
}
从理论上讲:数学函数得到输入变量,对变量进行一些变换,并输出变换的结果。
例如:f(x) = x - 1
、g(x) = x * 2
您可以以一种函数输出将是另一个函数输入的方式链接函数:g(f(2))
。在这种情况下,数字 2 用作函数 f(x)
的输入,f(x)
的输出是 g(x)
.
的输入
编程中的函数和方法可以以类似的方式工作,但将函数输出保存为有意义的变量名,然后将这些变量应用于下一个功能。
而不是做:outputText(processText(readText(someFilename)))
你可以写(伪代码):
someFilename = 'foo'
text = readText(someFilename)
processed = processText(text)
outputText(processed)
在 java 和您的上下文中,这将如下所示:
public class Test1 {
public static void main(String[] args) {
String nameFile = "numbers.txt";
String contentFile = readFromFileByName(nameFile);
String altered = processText(contentFile);
saveToFile(altered, "processed.txt");
}
private static String readFromFileByName(String nameFile) {
String fullRead = "";
try {
File file = new File(nameFile);
BufferedReader read = new BufferedReader(new FileReader(file));
String line; // define line variable
while((line = read.readLine()) != null) {
fullRead += line; // pay attention for the altered code
}
read.close();
} catch (IOException e) {
System.out.println("There was an error reading from a file");
} finally {
return fullRead;
}
}
private static List<Integer> stringToIntList(String string) {
return Arrays
.stream(text.split(", "))
.map(Integer::parseInt)
.collect(Collectors.toList());
}
private static String processText(String text) {
String processed = text.replace('H', 'h'); // Some heavy processing :)
return processed;
}
private static void saveToFile(String text, String fileName) {
// save <text> to file with filename <filename>
}
}
1) Line
是你读到的变量。所以你不应该改变它的价值。
line = contentFIle;
如果您只需要第一行,此方法应如下所示:
private static String readFromFile(String nameFile) {
String line = "";
try {
BufferedReader read = new BufferedReader(new FileReader(nameFile));
line = read.readLine();
read.close();
} catch (IOException e) {
System.out.println("There was an error reading from a file");
}
return line;
}
如果您需要这样的列表:
List<String> lines = Collections.emptyList();
try {
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
} catch (IOException e) {
// do something
e.printStackTrace();
}
return lines;
2) 也不要调用 readFromFile 函数。所以你需要改变主要方法:
public static void main(String[] args) {
String nameFile = "numbers.txt";
String contentFile = readFromFile(nameFile);
}
3) 对于您的特定情况,使用 String contentFile
调用 readFromFile 是没有意义的,因为您不使用此变量。
我是 Java 编程的初学者(最近开始学习),我需要帮助。
我必须从一个包含数字的文件中读取。我想制作一种从文件中读取的方法。然后我需要分析这些数据并将其写入另一个文件。
我苦恼的是,如果我创建一个方法只是为了从文件中读取数据,还是我还必须将读取的数据保存到一个变量中。这个变量应该在方法内部的什么地方声明(如果在内部,我如何在外部使用它),如果在外部,我如何在方法内部和外部使用它。谁能帮我澄清一下?我究竟做错了什么?
到目前为止我写的代码。我必须阅读的文件有成百上千的数字。
public class Test1 {
public static void main(String[] args) {
String nameFile = "numbers.txt";
File file = new File(nameFile);
String contentFile ="";
}
//Method for reading a .txt file
private static String readFromFile(String nameFile, String contentFile) {
String line = "";
try {
BufferedReader read = new BufferedReader(new FileReader(nameFile));
while((line = read.readLine()) != null) {
line = contentFIle;
}
read.close();
} catch (IOException e) {
System.out.println("There was an error reading from a file");
}
return line;
}
}
从理论上讲:数学函数得到输入变量,对变量进行一些变换,并输出变换的结果。
例如:f(x) = x - 1
、g(x) = x * 2
您可以以一种函数输出将是另一个函数输入的方式链接函数:g(f(2))
。在这种情况下,数字 2 用作函数 f(x)
的输入,f(x)
的输出是 g(x)
.
编程中的函数和方法可以以类似的方式工作,但将函数输出保存为有意义的变量名,然后将这些变量应用于下一个功能。
而不是做:outputText(processText(readText(someFilename)))
你可以写(伪代码):
someFilename = 'foo'
text = readText(someFilename)
processed = processText(text)
outputText(processed)
在 java 和您的上下文中,这将如下所示:
public class Test1 {
public static void main(String[] args) {
String nameFile = "numbers.txt";
String contentFile = readFromFileByName(nameFile);
String altered = processText(contentFile);
saveToFile(altered, "processed.txt");
}
private static String readFromFileByName(String nameFile) {
String fullRead = "";
try {
File file = new File(nameFile);
BufferedReader read = new BufferedReader(new FileReader(file));
String line; // define line variable
while((line = read.readLine()) != null) {
fullRead += line; // pay attention for the altered code
}
read.close();
} catch (IOException e) {
System.out.println("There was an error reading from a file");
} finally {
return fullRead;
}
}
private static List<Integer> stringToIntList(String string) {
return Arrays
.stream(text.split(", "))
.map(Integer::parseInt)
.collect(Collectors.toList());
}
private static String processText(String text) {
String processed = text.replace('H', 'h'); // Some heavy processing :)
return processed;
}
private static void saveToFile(String text, String fileName) {
// save <text> to file with filename <filename>
}
}
1) Line
是你读到的变量。所以你不应该改变它的价值。
line = contentFIle;
如果您只需要第一行,此方法应如下所示:
private static String readFromFile(String nameFile) {
String line = "";
try {
BufferedReader read = new BufferedReader(new FileReader(nameFile));
line = read.readLine();
read.close();
} catch (IOException e) {
System.out.println("There was an error reading from a file");
}
return line;
}
如果您需要这样的列表:
List<String> lines = Collections.emptyList();
try {
Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
} catch (IOException e) {
// do something
e.printStackTrace();
}
return lines;
2) 也不要调用 readFromFile 函数。所以你需要改变主要方法:
public static void main(String[] args) {
String nameFile = "numbers.txt";
String contentFile = readFromFile(nameFile);
}
3) 对于您的特定情况,使用 String contentFile
调用 readFromFile 是没有意义的,因为您不使用此变量。