將登陸人ip寫入txt檔案,登陸ip寫入txt

來源:互聯網
上載者:User

將登陸人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
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.