java讀取指定檔案內容代碼

來源:互聯網
上載者:User

java讀取指定檔案內容代碼
<%@ page contenttype="text/html; charset=gb2312" language="java" import="java.sql.*" errorpage="" %>
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>java讀取指定檔案內容代碼</title>
</head>

<body>

<%
try {
    // create the reader
    string filename = "infile.txt";
    string patternstr = "pattern";
    bufferedreader rd = new bufferedreader(new filereader(filename));

    // create the pattern
    pattern pattern = pattern.compile(patternstr);
    matcher matcher = pattern.matcher("");

    // retrieve all lines that match pattern
    string line = null;
    while ((line = rd.readline()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            // line matches the pattern
        }
    }
} catch (ioexception e) {
}
%>
</body>
</html>

//執行個體二

/**
* 輸入內容、起始位置、結束位置、尋找第幾處後,找出該處位置的內容
* 
* @param scontent
* @param sstart
* @param send
* @param istart
* @return sout
*/
private string getstrbykey(string scontent, string sstart, string send,
int istart) {
string sout = null;
int fromindex = 0;
int ibegin = 0;
int iend = 0;

if (null == scontent || scontent.equals("")) {
return null;
}
if (null == sstart || sstart.equals("")) {
return null;
}
if (null == send || send.equals("")) {
return null;
}
if (istart < 0) {
return null;
}
try {
for (int i = 0; i < istart; i++) {
// 找出某位置,並找出該位置後的最近的一個匹配
ibegin = scontent.indexof(sstart, fromindex);
if (ibegin >= 0) {
iend = scontent.indexof(send, ibegin + sstart.length());
if (iend <= ibegin) {
return null;
}
} else {
return null;
}

if (iend > 0) {
sout = scontent.substring(ibegin + sstart.length(), iend);
}

if (iend > 0) {
fromindex = iend + send.length();
}
}
} catch (exception e) {
system.out.println("解析內容出現異常:" + e);
sout = null;
}
return sout;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.