Проблема с местоположением файла .txt в NetBeansJAVA

Программисты JAVA общаются здесь
Anonymous
 Проблема с местоположением файла .txt в NetBeans

Сообщение Anonymous »

В настоящее время я работаю над проектом, в котором есть его часть, который читает и пишет из файла. Все в моем коде работает, но я не хочу использовать абсолютный путь для программы, чтобы найти файл, потому что я хочу, чтобы он работал на всех устройствах. Однако, когда я помещаю файл в каталог с моим исходным кодом с помощью нового файла -автора ("pokemon.txt"); Он не может обнаружить файл, это идет на то, когда я помещаю его в корневую папку. Как мне исправить это, я искал везде, и я понятия не имею, что делать. < /P>
public static void pokeWrite() {
//Writing to a file

String filePath = "Pokemon";
//Creates Scanner
Scanner scnr = new Scanner(System.in);

//Takes user input for length of array

System.out.println("How many pokemon do you want to add?");
int pokeAmount = scnr.nextInt();

//Creates the Array
String[] pokeArr = new String[pokeAmount];

//Takes user input for the Strings depending on the size of the array
System.out.println("Please enter these Pokemon now");
for(int i = 0 ; i < pokeAmount; i++){
pokeArr = scnr.next();
}

try{

//Uses Buffered writer to check the location file and see if the file exists.
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true));

//Writes to the file with the inputed value and creates a new line.
//Loops through the array adding each string the user inputed to the file.
for (String pokemon : pokeArr){
writer.write(pokemon + "\n");
//Closes writer.

}
//Closes writer.
writer.close();
}
//Catch exception in case file cant be found.
catch (IOException e){
e.printStackTrace();
}
}
< /code>
public static String[] pokeRead() {

int pokeAmountRead = pokeLineCount();//Uses the pokeLineCount() function to aqquire the amount of lines in the file.
String[] pokeReadArr = new String[pokeAmountRead];//Creates an array using the pokeAmountRead int.

try{

BufferedReader reader = new BufferedReader(new FileReader("Pokemon.txt"));
//Creates reader to take information in from the file.

String pokeLine;//Creation of string value to be used later on.

// System.out.println("Number of lines: " + pokeAmountRead); //Used for testing to see amount of lines in file

int pokeCount = 0;//Creates Counter for while loop.

while((pokeLine = reader.readLine()) != null){ //Calls pokeLine to store each line of the file until the file reaches its end.

pokeReadArr[pokeCount] = pokeLine;//Places value from file in the array designated by the counter

// System.out.println(pokeReadArr[pokeCount] + "This is in the while loop in pokeRead"); //Testing to see if the array is getting the values.
pokeCount++;//Increases counter
}

}
catch (IOException e){
e.printStackTrace();//Shows the information on the throwable if one occurs.
}
return pokeReadArr;//returns the array
}
< /code>
If Anyone knows how to fix this please let me know.

Подробнее здесь: https://stackoverflow.com/questions/794 ... -net-beans

Вернуться в «JAVA»