Results 1 to 5 of 5

Thread: IO stream

  1. #1
    sadeem is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default IO stream

    hi ^_^
    I have code to check if the file or directory can be read i'll display them content, and to check if they can be write so write to file extra sentence but for directory , create a new directory with the name "newDirectory" and create a new file called "newFile" inside that new directory "newDirectory". Otherwise, writing is not permitted and you display "Sorry we can't" on screen. then Rename the file newFile and give it a new name " oldFile"....

    so I try to write simple code, it work with file but not with directory I don't know why??

    can anyone help me to make the code work well ^_^

    Java Code:
    import java.util.*;
    import java.io.*;
    import java.util.Date;
    
    public class Q1 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    Scanner scan=new Scanner(System.in);
    System.out.println("enter file or folder pathname :");
    String s=scan.next();
    try{
    	File file=new File(s);
    	
    	
    	if(file.exists()!=true)
    		System.out.println("File or directory does not exist-Bye");
    		else if(file.isFile()&&file.canRead()){
    			BufferedReader input=new BufferedReader(new FileReader(s));
    			String ch;
    			while((ch=input.readLine()) != null)
    			System.out.println(ch);}
    		    else if(file.isDirectory()&&file.canRead()){String [] sl=file.list();
    		    for (int i=0; i<sl.length;i++)
    		    System.out.println(sl[i]);}	
    		  
    	        if(file.isFile()&&file.canWrite()){
    			BufferedWriter bw=new BufferedWriter(new FileWriter(s,true));
    			bw.newLine();
    			bw.write("Last but not least");
    			bw.close();
    			}
    		    else if(file.isDirectory()&& file.canWrite()){
    		    	File fd=new File(s);
    		    fd.mkdir();	
    			
    			
    			}else System.out.println("sorry");
    	
    	File file2=new File("newFile.txt");
    	file2.renameTo(new File("oldFile.txt"));
    	
    			
    		}//end try
            catch(FileNotFoundException ex){
    			System.err.println(ex.getMessage());
    		}//end catch
    		catch(IOException ex){
    			System.err.println(ex.getMessage());
    		}//end catch
    
    	
    	
    }//end main 
    
    	
    	}//end class

  2. #2
    leen's Avatar
    leen is offline Member
    Join Date
    Mar 2011
    Posts
    16
    Rep Power
    0

    Default

    can anyone help me to make the code work well ^_^
    i can't explain to you
    but you need to check the main class\

    please help her


    good luck
    I did not understand anything in Java:(
    Therefore, future lost:eek:

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It works except for the mkdir part because you have to change s for the directory to be a new one. e.g.,

    Java Code:
             else if (file.isDirectory() && file.canWrite()) {
                File fd = new File(s + "/foo"); // **** note change here ****
                fd.mkdir();
             } else

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by leen View Post
    i can't explain to you
    but you need to check the main class\
    What do you mean by "check the main class"? Sorry but this doesn't make sense.

  5. #5
    sadeem is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    tank you all..
    i want to create the new directory in the same path that user enter it , and in this new dir create new file.
    i try to use
    File class which its constructor..
    Java Code:
    File(File parent, String child)
    but the code is still not work with directory..

Similar Threads

  1. How do I know that byte of stream is finished?
    By mani_minhaj in forum New To Java
    Replies: 2
    Last Post: 02-26-2010, 12:08 PM
  2. TCP stream cypher
    By Koren3 in forum Networking
    Replies: 2
    Last Post: 05-20-2009, 10:08 PM
  3. stream redirection
    By Ed in forum New To Java
    Replies: 1
    Last Post: 07-02-2007, 04:34 PM
  4. video stream
    By Alan in forum Advanced Java
    Replies: 2
    Last Post: 05-17-2007, 08:12 AM

Posting Permissions

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