I am trying to make a Java variant of bobybee's DNSMagic, called DNSauce. Currently it works, but it deletes all existing content of the hosts file, instead of omitting comments and localhost entries. Can someone help me here?
package org.thegt.dnsauce.io; import java.io.*; import java.util.Scanner; import org.thegt.dnsauce.*; @SuppressWarnings("resource") public class DNSIO { private static File hostsFile; private static FileInputStream hostsInput; private static FileOutputStream hostsOutput; static { try { hostsFile = new File("C:\\Windows\\System32\\drivers\\etc\\hosts"); hostsInput = new FileInputStream(hostsFile); hostsOutput = new FileOutputStream(hostsFile); writeExisting(); } catch (IOException e) { try { hostsFile = new File("/etc/hosts"); hostsInput = new FileInputStream(hostsFile); hostsOutput = new FileOutputStream(hostsFile); writeExisting(); } catch (IOException ex) { System.err.println("Hosts file not found."); System.exit(1); } } } public static void add (DNSEntry entry) throws IOException { Scanner s = new Scanner(hostsInput); s.useDelimiter("\\A"); String hostsFileContents = s.hasNext() ? s.next() : ""; if (!hostsFileContents.contains(entry.toString()) && !entry.getHost().trim().toLowerCase().equals("localhost")) { new PrintStream(hostsOutput).println(entry.toString()); } } public static void add (DNSEntry[] entries) throws IOException { for (DNSEntry entry : entries) { add(entry); } } public static void remove (DNSEntry entry) throws IOException { Scanner s = new Scanner(hostsInput); String newHostsFile = ""; while (s.hasNextLine() && !entry.getHost().trim().toLowerCase().equals("localhost")) { String line = s.nextLine(); if (line != entry.toString()) { newHostsFile += line+ "\n"; } } FileOutputStream eraser = new FileOutputStream(hostsFile); eraser.write(new String().getBytes()); eraser.close(); hostsOutput.write(newHostsFile.getBytes()); } public static void remove (DNSEntry[] entries) throws IOException { for (DNSEntry entry : entries) { remove(entry); } } public static void clear () throws IOException { Scanner s = new Scanner(hostsInput); while (s.hasNextLine()) { String line = s.nextLine(); if (!line.contains("#")) { remove(new DNSEntry(line)); } } } public static void write(DNSEntry[] entries) throws IOException { clear(); add(entries); } private static void writeExisting () throws IOException { Scanner s = new Scanner(hostsInput); s.useDelimiter("\\A"); String existing = s.hasNext() ? s.next() : ""; byte[] data = existing.getBytes(); if (existing.length() < 1) { System.out.println("WARNING: Hosts file is empty!"); } hostsOutput.write(data); } }
Last edited by GeonoTRON2000 (2012-10-29 23:31:41)
Offline
DigiTechs wrote:
wait,
I believe this has something to do with it:
hostsFile = new File();
I belive Java is trying to make a new File, as "new File" is there.
That just creates a new file object which represents the file on the file system. To create a new file you would use:
new File(name).createNewFile();
In this case, the file already exists, so that code wouldn't work. You'd either have to clear it or delete it then recreate it (not recommended, as other programs may need to use it whilst it is non-existant).
Anyway, to append to a file, you can use this:
new FileOutputStream(filename, true);
The second argument "true" indicates that you want to append to the file.
I'm not sure that's what you're after, though...
Offline
DigiTechs wrote:
It should be what he's after, as append mode should not overwrite the current data.
Thanks!
Offline