Results 1 to 4 of 4

Thread: Problem here!

  1. #1
    newbieph is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default Problem here!

    What's wrong in this code the first time i run this it's OK but when i closed the notepad and rerun the program there's an error occured:

    PHP Code:
    import java.io.*;
    
    public class WriteFile{
    
        public static void main(String[] args) throws IOException{
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    		
    	
          File f=new File("textfile1.txt");
          FileOutputStream fop=new FileOutputStream(f);
    	  System.out.print("Enter test: ");
    	String test1 = input.readLine();
    	      
    		  System.out.print("Enter test");
    	String test2 = input.readLine();
    		  System.out.print("Enter test:");
    	String test3 = input.readLine();
    		  System.out.print("Enter test:");
    	String test4 = input.readLine();
    		  System.out.print("Enter test:");
    	String test5 = input.readLine();
    		  System.out.print("Enter test:");
    	String test6 = input.readLine();
    		  System.out.print("Enter test:");
    	String test7 = input.readLine();
    		  System.out.print("Enter test:");
    	String test8 = input.readLine();
    		  System.out.print("Enter test:");
          if(f.exists()){
          String str= test1;
    	        String str2= test2;
    			      String str3= test3;
    				        String str4= test4;
    						      String str5= test5;
    							        String str6= test6;
    									      String str7= test7;
    										        String str8= test8;
    												
              fop.write(str.getBytes());
    		  fop.write(str2.getBytes());
    		  fop.write(str3.getBytes());
    		  fop.write(str4.getBytes());
    		  fop.write(str5.getBytes());
    		  fop.write(str6.getBytes());
    		  fop.write(str7.getBytes());
    		  fop.write(str8.getBytes());
    
              fop.flush();
              fop.close();
              System.out.println("The data has been written");
              }
    
              else
                System.out.println("This file is not exist");
        }
      }
    Last edited by Fubarable; 03-26-2011 at 02:56 PM. Reason: code tags added

  2. #2
    newbieph is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    here's the snapshot of the error:
    Attached Thumbnails Attached Thumbnails Problem here!-pic.jpg  
    Last edited by newbieph; 03-26-2011 at 02:47 PM.

  3. #3
    BOJANM is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default try this

    File f=new File(C:\..\..\file.txt)
    PrintStream prinstr=new PrintStream(f);

    Scanner sc=new Scanner(System.in);

    String input=sc.nextLine();
    if (f.exists()) ....
    printstr.println(input);

    printstr.close;

    I think problem is to file path
    must C:\..\..\file.txt - use \\ for \

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    I think problem is to file path
    Not at all. Did you look at the error messages at all?

    @ newbieph:

    1. Don't post images that end up all blurry, copy and paste the contents of the command prompt.

    2. Update your Java version, 1.4.2 was EOLed a long time ago.

    3. Get rid of your own class named File, or otherwise disambiguate between that and java.io.File.

    4. Go through Code Conventions for the Java(TM) Programming Language: Contents and learn how to indent code correctly.

    5. Why do you have a whole set of duplicate references to the same Strings?

    db

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •