-2
import javax.swing.*;
import java.io.*;
public class FileIO{
public static void main (String args [])throws IOException{

String input = JOptionPane.showInputDialog("Enter FileName: ");
File x = new File(input);    

File f = new File ("d:\\aa\\bb\\");
f.mkdirs();


FileWriter text = new FileWriter (f + "\\grades.txt");
text.write("90");  
text.write("91"); 
text.write("92"); 
text.write("93"); 
text.write("94"); 

text.close();

   if (f.exists()){
       System.out.println(f.getPath() + " exist");
   } else {
       System.out.println(f.getPath() + " does not exist");
   }



     //getting the average of the input grades from grades.txt   
      /* int sum = 0, ctr=0;

        while(val != null){
        ctr++;
        sum += Integer.parseInt(val);
        val = x.readLine();
        }
        System.out.println("Ave = " + sum/ctr);

        f.close();*/




}  
} 

Hello. I like to ask for your help. I need to check if the file input by the user is on the folder. And afterwards use the content of that so compute for the next set of codes. For example, a list of grades was input on the notepad. Example:

grades.txt: 90 91 92 93 94

Sample Output:

grades.txt exist in the directory

Grade Average: Ave = 92

2 Answers2

0

Using java.io.File:

String textFieldValue = testField.getText();

        File f = new File(textFieldValue );
        if(f.exists() && !f.isDirectory()) { 
         System.out.println(f.getPath() + " exist");
       } else {
           System.out.println(f.getPath() + " does not exist");
       }
VERYNET
  • 531
  • 2
  • 14
  • I don't really understand how .exists() and .isDirectory() works. – Black Husky Oct 06 '15 at 14:52
  • @BlackHusky check this for .exists() : http://www.tutorialspoint.com/java/io/file_exists.htm and this for .isDirectory() : http://www.tutorialspoint.com/java/io/file_isdirectory.htm – VERYNET Oct 06 '15 at 14:55
  • how about the file name that was input by the user? How will it check in the program? – Black Husky Oct 06 '15 at 14:55
  • @BlackHusky if an user input the file name from textfield or something , you just have to get this value and replace your original value writed in your code by this one. – VERYNET Oct 06 '15 at 14:57
  • @BlackHusky check this link to retrieve text from textfield : http://stackoverflow.com/questions/5752307/how-to-retrieve-value-from-jtextfield-in-java-swing – VERYNET Oct 06 '15 at 15:01
  • Getting the textfield is really complicated stuff for me. I'm still a beginner :( Sorry – Black Husky Oct 06 '15 at 15:06
  • @BlackHusky np i edited my code to show you how retrieve something from textfield and put it as parameter file . – VERYNET Oct 06 '15 at 15:08
0

Not sure what you are getting at, but this:

File f = new File ("d:\\aa\\bb\\");
f.mkdirs();

FileWriter text = new FileWriter (f + "\\grades.txt");

will try to create a FileWriter for the file d:\aa\bb\\grades.txt. Note the \\. Instead use:

FileWriter text = new FileWriter (new File(f, "grades.txt"));
NickJ
  • 9,380
  • 9
  • 51
  • 74