Results 1 to 3 of 3
  1. #1
    danborgir is offline Member
    Join Date
    Jan 2011
    Rep Power

    Default FileWriter editing hosts file Mac

    Im trying to edit the hosts file on Mac in my program and when I try to edit I get a message:

    Java Code: /private/etc/hosts.tmp (Permission denied)
    The code I am using is:

    Java Code:
    try {
    			FileWriter fstream = new FileWriter("/private/ect/hosts", true);
    			BufferedWriter out = new BufferedWriter(fstream);
    			out.write("\n178.63.127.253 \n178.63.127.253\n178.63.127.253");
    I am guessing that java does not have permissions to edit the file. Can anyone confirm this for me th/ is there anything I can do?


  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Rep Power


    well the typo'd "ect" folder would not exist, unless you typo'd it just for the post and yor code really ues "etc".

    but also, the system will require super user privileges to do this. for example, if you were to do this in a terminal, with "echo '...' > /private/etc/hosts " it would also complain.

    however, doing a "sudo echo ......" would work, but it would interactively ask you for a password.

    maybe if you are really wanting to break your hosts file you could try to run
    "sudo java -jar"
    This might invoke the file operation with the elevated privileges.

    But other concerns I have with doing this
    - your code just writes out these values to the hosts file, it will erase any existing contents in the hosts file right.
    - why would you want to have resolve to some "facebook blocked" response page by doing this on the individual machine.

    For blocking of a specified site(s) it is usually more effective to do this on the network gateway, such as on your router, running a transparent HTTP proxy (e.g. squid) with a regular expression URL rule plugin such as squidGuard. or possibly from a leser extent faking the DNS server with a bunch of fake entries in a .zone that all point to this black hole page.

    Aside from either of these soltuions not being a fun Java based solution, they really are (from my network administrator experience) the only fully effective and easy to maintain way to do it. gateway control is cross platform, in that any device installed inside your network (even ones you have not gotten around to setting up yet, such as windows machines, mobile phones, and new machines you have not yet locked down, users personal machines they bring in). and, no one inside your organization could circumvent it (by whacking their hosts file).

  3. #3
    danborgir is offline Member
    Join Date
    Jan 2011
    Rep Power


    Hi travishein,

    Thank you for the detailed response! I'll go into a little detail of why I wanted to use the hosts file:

    I am developing a program called Faceblock, its for students who waste too much time on facebook when they should be studying! Being a student myself I know how hard it can be to just turn it off and study! I am studying Computer Network management & design so if it were possible to use a HTTP proxy I would however this wont be possible!

    But thank you for your info I will struggle on!

Similar Threads

  1. BufferedWriter, FileWriter
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 03:35 AM
  2. write to file with filereader/filewriter
    By LennyKosmos in forum New To Java
    Replies: 3
    Last Post: 10-27-2010, 09:12 PM
  3. FileWriter doesn't write
    By superman5 in forum NetBeans
    Replies: 2
    Last Post: 07-30-2009, 06:46 AM
  4. JAva Filewriter
    By tommyyyy in forum New To Java
    Replies: 1
    Last Post: 03-28-2009, 01:51 PM
  5. Replies: 1
    Last Post: 07-24-2007, 12:58 AM

Posting Permissions

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