Text files on a pure JSP operation server [2]

Source: Internet
Author: User
Tags readline
js| Server

Read data on one line

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> file Read </title>
<body>
<%
String Path=request.getrealpath ("");//Get the path to the current directory
FileReader fr=new FileReader (path + "\\file\\inc\\t.txt");//Create the FileReader object and instantiate it as FR
BufferedReader br=new BufferedReader (FR);//Establish BufferedReader object and instantiate as BR
String Line=br.readline ()//reading a line from a file
Determines whether the read string is not empty
while (Line!=null) {
Out.println (line + "<br>")//Output data read from file
Line=br.readline ()//Continue reading a row of data from a file
}
Br.close ()//Close BufferedReader Object
Fr.close ()//close file
%>
</body>

Skipping characters in a file does not read

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> Skip bytes Not read </title>
<body>
<%
String Path=request.getrealpath (".");
FileReader fr=new FileReader (path + "\\ReadData.txt");
Fr.skip (2);//Skip 2 bytes
int C=fr.read ()//read one byte
while (C!=-1) {
Out.print ((char) c);
C=fr.read ();
}
Fr.close ();
%>
</body>

Writing data to a file

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> Write data to file </title>
<body>
<%
String Path=request.getrealpath (".");
FileWriter fw=new FileWriter (path + "\\WriteData.txt");//Create FileWriter object and instantiate FW
Writing a string to a file
Fw.write ("Hello, everyone!") ");
Fw.write ("This book is the JSP Programming skill");
Fw.write ("Please advise!") ");
Fw.write ("email:stride@sina.com");
Fw.close ();

FileReader fr=new FileReader (path + "\\WriteData.txt");
BufferedReader br=new BufferedReader (FR);//Establish BufferedReader object and instantiate as BR
String Line=br.readline ();
Reading a row of data
Out.println (line + "<br>");
Br.close ()//Close BufferedReader Object
Fr.close ();
%>
</body>

The data branch that will be written to the file
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> data to be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
FileWriter fw=new FileWriter (path + "\\WriteData.txt");
BufferedWriter bw=new BufferedWriter (FW);
Bw.write ("Hello, everyone!") ");
Bw.write ("This book is the JSP Programming technique".) ");
Bw.newline ()/Break
Bw.write ("Please advise!") ");
Bw.newline ()/Break
Bw.write ("email:stride@sina.com");
Bw.flush ()//Update data to File
Fw.close ()//close file stream
Out.println ("Write file content is:<br>");
FileReader fr=new FileReader (path + "\\WriteData.txt");
BufferedReader br=new BufferedReader (FR);
String line=br.readline ();//reading a row of data
while (Line!=null) {
Out.println (line + "<br>");
Line=br.readline ();
}
Fr.close ();
%>
</body>

How to append data to a file

<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.io.*"%>
<title> data to be written to the file branch </title>
<body>
<%
String Path=request.getrealpath (".");
Randomaccessfile rf=new randomaccessfile (path + "\\WriteData.txt", "RW");//define object for a class Randomaccessfile and instantiate
Rf.seek (Rf.length ());//move pointer to end of file
Rf.writebytes ("\nappend a line to the file!");
Rf.close ()//close file stream
Out.println ("Write file content is:<br>");
FileReader fr=new FileReader (path + "\\WriteData.txt");
BufferedReader br=new BufferedReader (FR);//Bufferedread object to read files
String Line=br.readline ();
while (Line!=null) {
Out.println (line + "<br>");
Line=br.readline ();
}
Fr.close ()//close file
%>
</body>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.