Write the logon ip address to the txt file and the logon ip address to the txt file.
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;
}
How to Write a txt file in java
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)
{
Optional SC = null;
PrintWriter pw = null;
Try {
If (! Log. exists () // if the file does not exist, create a new one.
{
File parentDir = new File (log. getParent ());
If (! ParentDir. exists () // If the directory does not exist, create a new one.
ParentDir. mkdirs ();
Log. createNewFile ();
}
SC = new round (log );
StringBuilder sb = new StringBuilder ();
While (SC. hasNextLine () // read the content of the old file and save it to sb;
{
Sb. append (SC. nextLine ());
Sb. append ("\ r \ n"); // The linefeed is used as the interval. The scanner cannot read it, so you must add it yourself.
}
SC. close ();
Pw = new PrintWriter (new FileWriter (log), true );
/* A. */pw. println (sb. toString (); //, write the content of the old file.
/* B. */pw. println (newLog); // write new logs.
/*
* If A is written first, the latest log is at the end of the file.
* If data is written to B first, the latest log is at the beginning of the file.
*/
Pw. close ();
}
Catch (IOException ex)
{
Ex. printStackTrace ();
}
}
}
What should I do if I want to write the Ping result to the txt file?
Ping *. *> d: \ ping.txt
Save the ping *. * ip to the d: \ ping.txt file.
Example: ping www.baidu.com> d: \ ping.txt
Open 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 = 21 ms TTL = 50
Reply from 202.108.22.43: bytes = 32 time = 21 ms TTL = 50
Reply from 202.108.22.43: bytes = 32 time = 21 ms TTL = 50
Reply from 202.108.22.43: bytes = 32 time = 21 ms TTL = 50
Ping statistics for 202.108.22.43:
Packets: Sent = 4, stored ED = 4, Lost = 0 (0% loss ),
Approximate round trip times in milli-seconds:
Minimum = 21 ms, Maximum = 21 ms, Average = 21 ms