This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-10-29 21:36:00

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Java File I/O Help

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?

Code:

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)


http://i.imgur.com/BAEgGDL.png

Offline

 

#2 2012-10-29 23:31:58

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: Java File I/O Help

Code edited, still doesn't work...


http://i.imgur.com/BAEgGDL.png

Offline

 

#3 2012-10-30 09:55:10

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: Java File I/O Help

bump


http://i.imgur.com/BAEgGDL.png

Offline

 

#4 2012-10-30 11:47:31

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Java File I/O Help

Is there an append mode of the i/o on java?

On C++ it's ios_base::app or somthing similar


I'm back.
Maybe.

Offline

 

#5 2012-10-30 12:37:13

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Java File I/O Help

wait,
I belive this has somthing to do with it:
hostsFile = new File();
I belive Java is trying to make a new File, as "new File" is there.


I'm back.
Maybe.

Offline

 

#6 2012-10-30 14:18:10

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Java File I/O Help

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...


/* No comment */

Offline

 

#7 2012-10-30 15:15:56

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Java File I/O Help

It should be what he's after, as append mode should not overwrite the current data.


I'm back.
Maybe.

Offline

 

#8 2012-10-30 18:51:26

GeonoTRON2000
Scratcher
Registered: 2009-12-24
Posts: 1000+

Re: Java File I/O Help

DigiTechs wrote:

It should be what he's after, as append mode should not overwrite the current data.

Thanks!


http://i.imgur.com/BAEgGDL.png

Offline

 

Board footer