Results 1 to 6 of 6
  1. #1
    suroh is offline Member
    Join Date
    Jul 2015
    Posts
    4
    Rep Power
    0

    Default Issues with opening a .txt file contained in a private variable

    Okay so I'm just messing around really trying to understand Java, My issue that I want to create a folder and a file that being said I want the path of the folder/file to be input by a user and stored in a variable at the moment I'm using a String and I understand it's better to use a path and I'll test that out next. So I can create the folder and the file with no issues the problem how ever is that I'm storing the path in two separate private Strings so I'm not sure how to go about accessing the private String that contains the file path so that I can write to the file.

    Java Code:
    class Setup { 
      private String Folder_Path; 
      private String File_Path; 
    
      	public String  SetFolder(String Folder_Path) {
    			this.Folder_Path = Folder_Path; 
    			  return Folder_Path;
    		}
    		
    		public String SetFile(String File_Path) {
    			this.File_Path = File_Path; 
    			return File_Path;
    		}
    		
    		
    		public String ShowFilePath() {
    			return File_Path;
    		}
    		
    		public String ShowFolderPath() {
    			return Folder_Path;
    		}  
    
     public static void main(String[] args) {
    
    MenuFunc();
    
    
    
    
    
    
    
    }
    
    public static void MenuFunc() {
    
       Scanner Input = new Scanner(System.in);
    	String Choice;
    	
    	System.out.println(" \n\n Setup \n\n Write Text");
    	Choice = Input.nextLine(); 
    
           if(Choice.equals("Setup")) { 
    		SetupFunc();
    	} 
    	
    	if(Choice.equals("Write Text")) {
    		WriteFunc();
    	} 
    
    
    }
    
    
    public static void SetupFunc() {
    
        String TempFolder = ""; 
    	String TempFile = "";
                Setup S1 = new Setup();
                   Setup S2 = new Setup();
    	
        Scanner S_Input = new Scanner(System.in); 
    	
    	System.out.println(" \n\nFirst were going to create a folder, Please input the location where you want it to be stored \n\n Windows example C:\\ExampleFolder ");
    	TempFolder = S_Input.nextLine(); 
    	System.out.println("the Folder path you've entered is: " + TempFolder);
    	
    	try {
    		
    		File D = new File(TempFolder);  
    		S1.SetFolder(TempFolder); 
    		
    		if(D.exists()) {
    			System.out.println("This Directory already exists");
    		} 
    		
    		else D.mkdir();
    	} 
    	
    	catch(Exception E) {
    		E.getMessage();
    		System.out.println(" ** Folder Creation Failed ** " + TempFolder + E );
    		
    	}
    	
    	System.out.println(" Now Were going to store a file in that folder, Please name your file and include .txt");
    	TempFile = S_Input.nextLine(); 
    	System.out.println("The File Path you entered is: " + TempFile); 
    	
    	try {
    		
    		File file = new File(TempFile); 
    		S2.SetFile(TempFile);
    		if(file.exists()) {
    			System.out.println(" Sorry but this file already exists ");
    			
    		}
    		
    		else file.createNewFile(); 
    	} 
    	catch(Exception E) {
    		E.getMessage();
    		System.out.println("** Error ** File Creation: " + TempFile + " ** Has Failed ** " + E);
    	} 
    
     MenuFunc();
    
    }
    
    public static void WriteFunc() {
    	
    	Setup S = new Setup();
    	
    	Scanner Information = new Scanner(System.in);
         
           String Text;	
    
    	String filepath = S.ShowFilePath();  
    
            try (PrintWriter Out = new PrintWriter(new BufferedWriter(new FileWriter(filepath, true)))) {
    		 System.out.println("\n Input  random text"); 
    		 Text = Information.nextLine();
    		 Out.println("Text : " + Text);
    
            } catch(IOException E) {
    		E.getMessage(); 
    		E.printStackTrace();
    		System.out.println("***ERROR*** Writing to the file has failed do to: " + E );
    	}
    	
    	MenuFunc();
    
    
    }
    
    }
    So like I said creating a file and folder is not an issue, how ever when I want to write to that file it can't find the path.
    Last edited by suroh; 10-28-2015 at 06:57 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Issues with opening a .txt file contained in a private variable

    how to go about accessing the private String that contains the file path
    Use a gettor method that returns the path. What is the name of the variable and what class is it in?

    Note: The methods: ShowFilePath and ShowFolderPath are gettor methods and show be named: getFilePath and getFolderPath.

    Naming standards for java programs suggest that the names of methods and variables start with lowercase letters and be camelCase (intermediate words start with caps)
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Issues with opening a .txt file contained in a private variable

    You need to either pass the Setup object to the write method or the actual path. Newly created Setup objects
    won't contain the information of previous objects by default. And please read up on Java naming conventions (see my signature below).

    In particular class names start with Capital letters. Methods and variables start with lower case letters. And method
    names and variables should have meaningful names. S for Setup is not meaningful. And ShowFilePath should
    be something like getFilePath.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    suroh is offline Member
    Join Date
    Jul 2015
    Posts
    4
    Rep Power
    0

    Default Re: Issues with opening a .txt file contained in a private variable

    Heh As some one coming from c++ it's not even worth my time the amount of over head and excess nonsense that needs to be written and the amount of people crying over conventional naming is beyond said. Thank you for not helpping me wont bother wasting my time with java in five years it will be obsolete any way lol. Bye now!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Issues with opening a .txt file contained in a private variable

    Yes, it's sad. After your PM in July, we're not sorry to be saying goodbye. Maybe this one is for real.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Issues with opening a .txt file contained in a private variable

    Quote Originally Posted by suroh View Post
    Thank you for not helpping me
    I see you have made a typo there. I think it sould read: Thank you for helpping (sic) me, but I still don't understand what the hell I am doing?

    Quote Originally Posted by suroh View Post
    wont bother wasting my time with java in five years it will be obsolete any way lol. Bye now!
    Yeah, maybe it's better this way. I know you'll be laughing at us when we're out of a job in five years from now.

    By the way, you really got me thinking here. This cannot end well for us Java developers. So this is what I will do: since you're coming from c++, and you're a shit-hot developer, it *must* be the way to go, so I have quit my job and started to learn c++. Thanks for helpping me!!
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. What is the use of marking Instance variable to private
    By rmurali.mcp in forum New To Java
    Replies: 2
    Last Post: 05-31-2011, 10:49 AM
  2. Replies: 3
    Last Post: 05-05-2011, 03:33 PM
  3. Replies: 1
    Last Post: 12-09-2009, 12:57 AM
  4. loading files contained within app jar file
    By thorne_ in forum New To Java
    Replies: 3
    Last Post: 05-18-2009, 02:26 PM
  5. Variable issues!
    By Alex89 in forum New To Java
    Replies: 1
    Last Post: 11-07-2007, 07:51 PM

Posting Permissions

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