最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Issue with .txt file location in Net Beans - Stack Overflow

programmeradmin2浏览0评论

I am currently working on a project that has a part of it that reads and writes from a file. Everything in my code works but I don't want to use an absolute path for the program to find the file because I want it to work on all devices. However when I put the file in the directory with my source code using new FileWriter("Pokemon.txt"); it cannot detect the file, this goes for when I put it into the root folder as well. How do I fix this I've searched everywhere and I have no idea what to do.

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();
           }
    }
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
    }

If Anyone knows how to fix this please let me know.

发布评论

评论列表(0)

  1. 暂无评论