Results 1 to 2 of 2
  1. #1
    Vinny8978 is offline Member
    Join Date
    Apr 2012
    Location
    York, UK
    Posts
    1
    Rep Power
    0

    Default Cannot read a file From APPDATA, but can write to it

    Hi,
    I am having a problem reading a file from APPDATA, I have permission to do so as I can create and write to the file

    here is the code I am using:

    Java Code:
    static public String getContents(File aFile) {
            StringBuilder contents = new StringBuilder();
            try {
                BufferedReader input =  new BufferedReader(new FileReader(aFile));
                try {
                    String line;
                    while (( line = input.readLine()) != null){
                    contents.append(line);
                    }
                }
                finally {
                    input.close();
                }
            } catch (IOException ex){
            }
            return contents.toString();
        }
      
        static public void setContents(File aFile, String aContents) throws FileNotFoundException, IOException {
            if (aFile == null) {
            throw new IllegalArgumentException("File should not be null.");
            }
            if (!aFile.exists()) {
            throw new FileNotFoundException ("File does not exist: " + aFile);
            }
            if (!aFile.isFile()) {
            throw new IllegalArgumentException("Should not be a directory: " + aFile);
            }
            if (!aFile.canWrite()) {
            throw new IllegalArgumentException("File cannot be written: " + aFile);
            }
            Writer output = new BufferedWriter(new FileWriter(aFile));
            try {
            output.write( aContents );
            }
            finally {
            output.close();
            }
        }
    
        public static void main(String[] args) throws IOException {
            String uuid = "";
            String pcname = "";
            String pcusername = "";
            UUID id = new UUID();
            
            java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
    
            boolean mkdir = (new File(System.getenv("APPDATA") + "\\.okapinetwork").mkdir());
            
            if( mkdir ) {
                String fileName = System.getenv("APPDATA") + "\\.okapinetwork\\uuid.txt";
    
                File f = new File(fileName);
                if(!f.exists()){
                    f.createNewFile();
                    setContents(f, "UUID: " + id.toString() );
                    uuid = id.toString();
                }else{
                    String contents = getContents(f);
                    String[] UID = contents.split(": ");
                    uuid = UID[1];
                }
                System.out.print("UUID: " + uuid);
            }
    this basically creates a UUID when the app is opened for the first time and saves it to a file, then when the app is opened again at a later date it pulls the UUID from the file.

    Thanks in Advance for the help

    Vinny
    Last edited by Vinny8978; 04-27-2012 at 06:42 PM.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Cannot read a file From APPDATA, but can write to it

    The UUID constructor needs two parameters (see UUID (Java Platform SE 6)).

Similar Threads

  1. read and write arraylists to a file
    By corney_16 in forum New To Java
    Replies: 2
    Last Post: 04-20-2010, 12:50 PM
  2. Replies: 5
    Last Post: 02-05-2009, 11:28 AM
  3. Replies: 5
    Last Post: 01-20-2009, 10:08 AM
  4. Read and Write file
    By mrdestroy in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:11 PM
  5. File read/write problems
    By p900128 in forum New To Java
    Replies: 4
    Last Post: 06-27-2008, 01:15 AM

Tags for this Thread

Posting Permissions

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