You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetBeans项目中相对路径无法定位.txt文件的问题求助

NetBeans项目中相对路径无法定位.txt文件的问题求助

我现在在做一个项目,其中有一部分需要读写文件。代码本身逻辑都没问题,但我不想用绝对路径,这样程序才能在所有设备上正常运行。可是当我把文件和源代码放在同一个目录下,用new FileWriter("Pokemon.txt");的时候,程序找不到这个文件;把文件放到根目录也不行。我到处搜了都没解决办法,有没有人能帮我看看怎么处理?

我的代码:

pokeWrite方法:

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[i] = 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();
   }
}

pokeRead方法:

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
}

如果有人知道怎么解决的话,麻烦告诉我!

备注:内容来源于stack exchange,提问作者TylerDr16

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 03:18:02