將登陸人ip寫入txt檔案,登陸ip寫入txt
protected void Page_Load(object sender, EventArgs e)
{
string ip = System.Web.HttpContext.Current.Request.UserHostAddress;
WriteIpToTxt(ip);
}
public void WriteIpToTxt(string ip)
{
string strfile;
strfile = "asp.txt";
string strout;
strout = "";
StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(strfile), System.Text.Encoding.Default);
String input = sr.ReadToEnd();
sr.Close();
StreamWriter sw = new StreamWriter(Server.MapPath(strfile), false, System.Text.Encoding.Default);
sw.Write(input+"IP:");
sw.Write(ip+"\r\n");
sw.Close();
strout = input;
}
java怎寫入txt檔案
import java.io.*;
import java.util.*;
public class LogTest{
public static void main(String[] args)
{
File log=new File("user\\log\\login.log");
String newLog="UserName:Jim Green | Date:"+new Date()+" | IP:202.65.21.14";
appendLog(log,newLog);
}
public static void appendLog(File log,String newLog)
{
Scanner sc=null;
PrintWriter pw=null;
try{
if(!log.exists())//如果檔案不存在,則建立.
{
File parentDir=new File(log.getParent());
if(!parentDir.exists())//如果所在目錄不存在,則建立.
parentDir.mkdirs();
log.createNewFile();
}
sc=new Scanner(log);
StringBuilder sb=new StringBuilder();
while(sc.hasNextLine())//先讀出舊檔案內容,並暫存sb中;
{
sb.append(sc.nextLine());
sb.append("\r\n");//分行符號作為間隔,掃描器讀不出來,因此要自己添加.
}
sc.close();
pw=new PrintWriter(new FileWriter(log),true);
/*A.*/pw.println(sb.toString());//,寫入舊檔案內容.
/*B.*/pw.println(newLog);//寫入新日誌.
/*
* 如果先寫入A,最近日誌在檔案最後.
* 如是先寫入B,最近日誌在檔案最前.
*/
pw.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
我想把Ping的結果寫入txt檔案 問改怎辦?
ping *.*.*.* >> d:\ping.txt
把ping *.*.*.* IP的結果存到d:\ping.txt檔案
樣本:ping www.baidu.com >>d:\ping.txt
開啟 d:\ping.txt
Pinging www.a.shifen.com [202.108.22.43] with 32 bytes of data:
Reply from 202.108.22.43: bytes=32 time=21ms TTL=50
Reply from 202.108.22.43: bytes=32 time=21ms TTL=50
Reply from 202.108.22.43: bytes=32 time=21ms TTL=50
Reply from 202.108.22.43: bytes=32 time=21ms TTL=50
Ping statistics for 202.108.22.43:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 21ms, Maximum = 21ms, Average = 21ms