我尝试将两个数组和一个整数从不同的方法引入到写入文件的方法中。我尝试使用参数引入,但不起作用。希望你能帮上忙
这就是我从保存数组信息的方法中传递参数的方式。
write (NameX, ScoresArray, players); /这是使用传递的数组参数来创建文件的方法。
public static void write (String NameX [], int ScoresArray [], int players ) throws IOException
{
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
outputStream.println(players);
for (int i = 0; i < NameX.length; i++)
{
outputStream.println(NameX[i] + ScoresArray [i] );
}
outputStream.close();
}这是读取文件的方法
public static void reads () throws IOException
{
BufferedReader inStream = new BufferedReader(new FileReader("Genius.txt"));
// Read in first file entry as an integer - the number of names stored
int players = Integer.parseInt(inStream.readLine());
System.out.println(players);
// Create an array big enough
String [] NameX = new String[players];
String [] ScoresArray = new String[players];
// Now read in the names
for (int i = 0; i < NameX.length; i++)
{
NameX[i] = inStream.readLine();
ScoresArray[i] = inStream.readLine();
System.out.println(NameX[i]);
System.out.println(ScoresArray[i]);
}
inStream.close();
}请您告诉我哪里出错了,以及如何创建一个保存数组并稍后读取该文件的文件。
*编辑代码*
write(NameX,ScoresArray,players);
reads();
}
}//end of league table
public static void write (String NameX [], int ScoresArray [], int players ) throws IOException {
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
outputStream.println(players);
for (int i = 0; i < NameX.length; i++)
{
outputStream.println(NameX[i] +":"+ ScoresArray [i] );
}
outputStream.close();} public static void reads ()抛出新{ BufferedReader inStream =新BufferedReader(新FileReader("Genius.txt"));
// Read in first file entry as an integer - the number of names stored
int players = Integer.parseInt(inStream.readLine());
System.out.println(players);
// Create an array big enough
String [] NameX = new String[players];
String [] ScoresArray = new String[players];
// Now read in the names
for (int i = 0; i < NameX.length; i++)
{
String str = inStream.readLine();
String vals[] = str.split(":");
System.out.println(vals[0]+" "+vals[1]);
}
inStream.close();}
我仍然遇到同样的问题
发布于 2014-12-09 18:25:36
IOException是一个已检查的异常,而您的函数write (NameX, ScoresArray, players);是抛出IOException。所以你应该在caller中处理它。
try {
write (NameX, ScoresArray, players); //
}catch(IOException ie){
// do operation
}有关详细信息,请参阅:Lesson: Exceptions
发布于 2014-12-09 18:31:12
好吧,我希望你能找到这个:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
*
* @author manoj.sharma
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void write (String NameX [], int ScoresArray [], int players ) throws IOException
{
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
outputStream.println(players);
for (int i = 0; i < NameX.length; i++)
{
outputStream.println(NameX[i] +":"+ ScoresArray [i] );
}
outputStream.close();
}
public static void reads () throws IOException
{
BufferedReader inStream = new BufferedReader(new FileReader("Genius.txt"));
// Read in first file entry as an integer - the number of names stored
int players = Integer.parseInt(inStream.readLine());
System.out.println(players);
// Create an array big enough
String [] NameX = new String[players];
String [] ScoresArray = new String[players];
// Now read in the names
for (int i = 0; i < NameX.length; i++)
{
String str = inStream.readLine();
String vals[] = str.split(":");
System.out.println(vals[0]+" "+vals[1]);
}
inStream.close();
}
public static void main(String[] args) {
String [] names={"manoj","kushal"};
int [] scores = {10,20};
int p = 2;
try{
write(names,scores,p);
reads();
} catch(IOException e){System.out.println(e.getMessage());}
}
}我希望你已经找到了解决方案。
发布于 2014-12-09 18:52:55
也许你可以尝试使用java.lang.Exception而不是IOException,使用Exception应该覆盖所有可能抛出的异常类型。
public static void write (String NameX [], int ScoresArray [], int players ) throws Exception
{
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
outputStream.println(players);
for (int i = 0; i < NameX.length; i++){
outputStream.println(NameX[i] + ScoresArray [i] );
}
outputStream.close();
}然后使用:
try {
write (NameX, ScoresArray, players); //
}catch(IOException io){
//Handle an IO exception
}catch(Exception e){
//Handle Exception
}不过,我恐怕还没有时间来测试这一点。
https://stackoverflow.com/questions/27376364
复制相似问题