#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    326
    Rep Power
    9

    Read from a file?


    I know how to read from a file in Java, but reading an actual "file" is giving me problems. When I say file, I mean a file with no file type. I can open up and write to it, but when I try to read it it reads nothing. Here is my code:

    Code:
        public void EditHost()
        {
            try { 
                final String hostpath = "C:\\WINDOWS\\system32\\drivers\\etc\\hosts";
                FileReader fr = new FileReader(hostpath); 
                BufferedReader br = new BufferedReader(fr); 
                FileOutputStream fw = new FileOutputStream(hostpath, true);
                BufferedWriter out = new BufferedWriter(new FileWriter(hostpath));
                String record = ""; 
                String hostinfo = "";
                String content = "";
                
                while ( (record=br.readLine()) != null ) 
                { 
                    content = content + record + "\n";
                    if (record.contains("L2authd.lineage2.com")) 
                    {
                     hostinfo = record;
                    }
                } 
                if (hostinfo == null)
                {
                    System.out.println("im appending!");
                    out.append("71.193.216.28 L2authd.lineage2.com");
                } else {
                    if (!hostinfo.equals("71.193.216.28 L2authd.lineage2.com"))
                    {
                        System.out.println("im writing!");
                        content = content.replace(hostinfo, "71.193.216.28 L2authd.lineage2.com");
                        out.write(content);
                    }
                }
                out.close();
                br.close();
            } catch (IOException e) { System.out.println("File not found!");}  
        }
    Every windows user has that file so you can go check it out yourself. When it writes to it, it only writes the text I gave it because "content" is nothing because it will not read the file. Any ideas why it's doing this?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Remember that Windows locks files while they are in use so it's possible that the FileOutputStream is not being connected to the file.

    Also it's a pretty bad idea to have a file open for both reading and writing at the same time.

    I would open the file, read the contents, modify the contents, and then write them back to the file.

    Comments on this post

    • AliasXNeo agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    326
    Rep Power
    9
    Opening and writing at the same time screwed it up. I didn't realize I was actually doing that. Thanks for the help

IMN logo majestic logo threadwatch logo seochat tools logo