我有一堆代码,现在已经演变成一个功能齐全的基于控制台(主要是)的游戏。我现在很好奇,如果我想实现一个输入/输出函数,我是否必须在另一个文件中创建它,或者我可以将它放在与我的代码相同的类中。例如,我的讲师给出了一个编写用于保存姓名的fileIO的示例:
import java.io.*;
class savenames
{
public static void main(String[] params) throws IOException
{
PrintWriter outputStream = new PrintWriter(new FileWriter("mydata.txt"));
// Create an array with some sample names to store
String [] names = {"Paul", "Jo", "Mo"};
// Store the names from the array in the file, one name per line
for (int i = 0; i < names.length; i++)
{
outputStream.println(names[i]);
}
outputStream.close();
System.exit(0);
}
} 下面的代码(在不同的文件中)如下所示:
import java.io.*;
class readnames
{
public static void main(String[] params) throws IOException
{
BufferedReader inStream = new BufferedReader(new FileReader("mydata.txt"));
String [] names = new String[3];
System.out.println("The names in the file mydata.txt are:");
for (int i = 0; i < names.length; i++)
{
names[i] = inStream.readLine();
System.out.println(names[i]);
}
inStream.close();
System.exit(0);
}
} 我只是想知道是否有可能在同一个文件中做这两件事,因为我的代码有许多不同的方法,我不确定如何创建一个单独的方法来做这件事。谢谢。
编辑:也许我可以修改这个问题,让它更好一些。
我在我的棋盘游戏中有以下主要方法:
class newminip
{
public static void main (String[] params) throws IOException
{
numberPlayers();
int diceroll = dicethrow(6);
int[] scorep1 = scorearrayp1();
questions(diceroll, scorep1);
sort(scorep1);
System.exit(0);
}
.... insert code here ....
public static void exitmethod(int[] scorep1)
{
sort(scorep1);
for (int i = 0; i < 4; i++)
{
System.out.println("Player " + (i+1) + " scored " + scorep1[i] + "");
}
System.exit(0);
}
} //END class我想要一个能把分数保存到一个新的文本文件中的东西。我希望这已经让它变得更清晰了。
发布于 2015-12-05 21:19:00
是的,你可以在一个文件中完成。我已经为它创建了一个新类:
import java.io.*;
import java.util.ArrayList;
public class FileIO {
public static String[] readStringsFromFile(final String filename) throws IOException {
BufferedReader inStream = new BufferedReader(new FileReader(filename));
//Use ArrayList since you don't know how many lines there are in the file
ArrayList<String> lines = new ArrayList<String>();
String line;
//Read until you reach the end of the file
while ((line = inStream.readLine()) != null) {
lines.add(line);
}
inStream.close();
//Convert it back to a string array
return lines.toArray(new String[lines.size()]);
}
public static void writeStringsToFile(String[] lines, final String filename) throws IOException {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename));
for (int i = 0; i < lines.length; i++) {
outputStream.println(lines[i]);
}
outputStream.close();
}
public static void main(String[] args) {
//To test the methods:
//Create an array to write to the file
String[] linesToWrite = {"firstLine", "secondLine", "thirdLine"};
try {
//Write the strings to a file named "testfile.txt"
writeStringsToFile(linesToWrite, "testfile.txt");
//Read all lines of a file named "testfile.txt"
String[] readLines = readStringsFromFile("testfile.txt");
//Print out the read lines
for (String line : readLines) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("Error msg");
}
}
}本例中的主要方法只是测试,您可以删除它并将其他两个方法复制到您的类中。这可能不是执行文件io的最好或最有效的方法,但在您的示例中,这应该可以完成工作(:EDIT:因此,如果您只需要将整数读写到文件中,您可以使用如下代码:
import java.io.*;
import java.util.ArrayList;
public class FileIO {
public static Integer[] readIntegersFromFile(final String filename) throws IOException {
BufferedReader inStream = new BufferedReader(new FileReader(filename));
//Use ArrayList since you don't know how many lines there are in the file
ArrayList<Integer> integers = new ArrayList<Integer>();
String line;
//Read until you reach the end of the file
while ((line = inStream.readLine()) != null) {
//Parse integers form read string values
integers.add(Integer.parseInt(line));
}
inStream.close();
return integers.toArray(new Integer[integers.size()]);
}
public static void writeIntegersToFile(Integer[] lines, final String filename) throws IOException {
PrintWriter outputStream = new PrintWriter(new FileWriter(filename));
for (int i = 0; i < lines.length; i++) {
outputStream.println(lines[i]);
}
outputStream.close();
}
public static void main(String[] args) {
//To test the methods:
//Create an array to write to the file
Integer[] linesToWrite = {1, 100, 15};
try {
//Write the strings to a file named "testfile.txt"
writeStringsToFile(linesToWrite, "testfile.txt");
//Read all lines of a file named "testfile.txt"
Integer[] readLines = readStringsFromFile("testfile.txt");
//Print out the read lines
for (int line : readLines) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("Error msg");
}
}
}发布于 2015-12-05 21:04:48
您可以使用两个main方法中的代码创建一个Java文件,并在不需要时删除System.exit(0);。这样一来,一个程序就可以同时完成这两个任务。我建议您先写入文件,然后再尝试读取它。
但是,将所有这些都放在一个程序中会使文件的使用变得相当多余,在这种情况下,您可以只打印数组。
https://stackoverflow.com/questions/34105113
复制相似问题