www.webdeveloper.com
Results 1 to 6 of 6

Thread: cant seem to read file

  1. #1
    Join Date
    Feb 2007
    Posts
    66

    cant seem to read file

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

    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\\system.txt";
    
                File f = new File(fileName);
                if(!f.exists()){
                    f.createNewFile();
                    setContents(f, "UUID: " + id.toString() + ", PC_NAME: " + localMachine.getHostName() + ", LOGGED_IN_USER: " + System.getProperty("user.name") + "");
                    uuid = id.toString();
                    pcname = localMachine.getHostName();
                    pcusername = USER[1];
                }else{
                    String contents = getContents(f);
                    String[] parts = contents.split(", ");
                    String[] UID = parts[0].split(": ");
                    String[] PC = parts[1].split(": ");
                    String[] USER = parts[2].split(": ");
                    uuid = UID[1];
                    pcname = PC[1];
                    pcusername = USER[1];
                }
            }

  2. #2
    Join Date
    Feb 2007
    Posts
    66

    Angry

    Well thanks for the help, I think from now on I will go find a better community of people who will happily help with simple problems like this

  3. #3
    Join Date
    Jan 2009
    Posts
    3,346
    Are you getting any error messages? Don't want to take the time to look for errors in your code when a message would narrow that effort down to probable issues.

  4. #4
    Join Date
    Feb 2007
    Posts
    66
    I am using NETBEANS and this is all that is in the output when I run the application

    Code:
    run:
    log4j:WARN No appenders could be found for logger (com.jniwrapper.DefaultLibraryLoader).
    log4j:WARN Please initialize the log4j system properly.

  5. #5
    Join Date
    Jan 2009
    Posts
    3,346
    You might want to notify yourself in the event of an IO error:
    } catch (IOException ex){
    //might want to log or alert in some way here
    }

  6. #6
    Join Date
    Feb 2007
    Posts
    66
    thanks for your help, I actually changed the code a bit, then added toString() to the loaded parts, and its working now
    this is the final code

    Code:
            UUID id = new UUID();
    
            String uuid = id.toString();
            String pcname = localMachine.getHostName();
            String pcusername = System.getProperty("user.name");
            
            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: " + uuid + ", PCNAME: " + pcname + ", PCUSERNAME: " + pcusername);
                }else{
                    String contents = getContents(f);
                    String[] parts = contents.split(", ");
                    String[] UID = parts[0].split(": ");
                    String[] PC = parts[1].split(": ");
                    String[] NAME = parts[2].split(": ");
                    uuid = UID[1].toString();
                    pcname = PC[1].toString();
                    pcusername = NAME[1].toString();
                }
            }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles