java 读取本地hosts文件,java读取hosts,import java.
分享于 点击 6703 次 点评:45
java 读取本地hosts文件,java读取hosts,import java.
import java.io.*;public class ghosts { public static void main(String[] args) { String filename = "C:/Windows/System32/drivers/etc/Hosts"; String data = "127.0.0.1 loclahost webod.haowei.me" + "\\r\\n" + "203.208.46.210 google.com.hk www.google.com.hk"; file_put_contents(filename,data); } public static void file_put_contents(String filename, String content) { File files = new File(filename); if(files.exists()) { try{ FileOutputStream out = new FileOutputStream(files); byte by[] = content.getBytes(); out.write(by); out.close(); file_get_contents(filename); }catch(Exception e) { print("error: files write failed"); } } else { print("error: check that file exists"); } } public static void file_get_contents(String filename) { File files = new File(filename); if(files.exists()) { try{ FileInputStream in = new FileInputStream(files); byte byt[] = new byte[4096]; int length = in.read(byt); print(new String(byt,0,length)); }catch(Exception e) { print("error"); } } else { print("error: check that file exists"); } } public static void print(String args) { System.out.println(args); }}//该片段来自于http://byrx.net
用户点评