国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
純 jsp 操作服務(wù)器上的文本文件 [2]

一行一行讀取數(shù)據(jù)

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>文件讀取</title>
</head>
<body>
<%
 String path=request.getRealPath("");//取得當(dāng)前目錄的路徑
 FileReader fr=new FileReader(path + "\\file\\inc\\t.txt");//建立FileReader對象,并實(shí)例化為fr
 BufferedReader br=new BufferedReader(fr);//建立BufferedReader對象,并實(shí)例化為br
 String Line=br.readLine();//從文件讀取一行字符串
 //判斷讀取到的字符串是否不為空
 while(Line!=null){
  out.println(Line + "<br>");//輸出從文件中讀取的數(shù)據(jù)
  Line=br.readLine();//從文件中繼續(xù)讀取一行數(shù)據(jù)
 }
 br.close();//關(guān)閉BufferedReader對象
 fr.close();//關(guān)閉文件
%>
</body>
</html>

略過文件中的字符不讀取

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>略過字節(jié)不讀取</title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileReader fr=new FileReader(path + "\\ReadData.txt");
fr.skip(2);//跳過2個(gè)字節(jié)
int c=fr.read();//讀取一個(gè)字節(jié)
while(c!=-1){
 out.print((char)c);
 c=fr.read();
}
fr.close();
%>
</body>
</html>

將數(shù)據(jù)寫入文件

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>將數(shù)據(jù)寫入文件</title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileWriter fw=new FileWriter(path + "\\WriteData.txt");//建立FileWriter對象,并實(shí)例化fw
//將字符串寫入文件
fw.write("大家好!");
fw.write("本書是《JSP編程技巧》");
fw.write("請多多指教!");
fw.write("email:stride@sina.com");
fw.close();

FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);//建立BufferedReader對象,并實(shí)例化為br
String Line=br.readLine();
//讀取一行數(shù)據(jù)
out.println(Line + "<br>");
br.close();//關(guān)閉BufferedReader對象
fr.close();
%>
</body>
</html>

將寫入文件的數(shù)據(jù)分行

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>將寫入文件的數(shù)據(jù)分行</title>
</head>
<body>
<%
String path=request.getRealPath(".");
FileWriter fw=new FileWriter(path + "\\WriteData.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("大家好!");
bw.write("本書是《JSP編程技巧》。");
bw.newLine();//斷行
bw.write("請多多指教!");
bw.newLine();//斷行
bw.write("email: stride@sina.com");
bw.flush();//將數(shù)據(jù)更新至文件
fw.close();//關(guān)閉文件流
out.println("寫入文件內(nèi)容為:<br>");
FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);
String Line=br.readLine();//讀取一行數(shù)據(jù)
while(Line!=null){
 out.println(Line + "<br>");
 Line=br.readLine();
}
fr.close();
%>
</body>
</html>

如何將數(shù)據(jù)追加寫入到文件

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<html>
<head>
<title>將寫入文件的數(shù)據(jù)分行</title>
</head>
<body>
<%
String path=request.getRealPath(".");
RandomAccessFile rf=new RandomAccessFile(path + "\\WriteData.txt","rw");//定義一個(gè)類RandomAccessFile的對象,并實(shí)例化
rf.seek(rf.length());//將指針移動到文件末尾
rf.writeBytes("\nAppend a line to the file!");
rf.close();//關(guān)閉文件流
out.println("寫入文件內(nèi)容為:<br>");
FileReader fr=new FileReader(path + "\\WriteData.txt");
BufferedReader br=new BufferedReader(fr);//讀取文件的BufferedRead對象
String Line=br.readLine();
while(Line!=null){
 out.println(Line + "<br>");
 Line=br.readLine();
}
fr.close();//關(guān)閉文件
%>
</body>
</html>

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JSP文件操作
jsp,java文件操作(新建,刪除,讀取,寫入)
JAVA文件操作大全
IO流-復(fù)制文本文件的五種方法案例
JAVA IO及線程
day21
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服