Java Tutorial/JSP/File Save Load

Материал из Java эксперт
Перейти к: навигация, поиск

Index of Files

<%@ page import="java.io.*" %>
<HTML>
    <HEAD>
        <TITLE>Index of Files</TITLE>
    </HEAD>
    <BODY>
        <H1>Index of Files</H1>
        Click a file to open/download it...
        <% 
            String file = application.getRealPath("/"); 
            File file1 = new File(file);
            String [] fileNames = file1.list();
            File [] fileObjects= file1.listFiles();
        %>
        <UL>
        <%
            for (int i = 0; i < fileObjects.length; i++) {
                if(!fileObjects[i].isDirectory()){
        %>
        <LI>
          
          &nbsp;&nbsp;&nbsp;&nbsp;
          (<%= Long.toString(fileObjects[i].length()) %> bytes long)
        <%
                }
            }
        %>
        </UL>
    </BODY>
</HTML>





Read file

<%@ page import="java.io.*" %>
<HTML>
    <HEAD>
        <TITLE>The file</TITLE>
    </HEAD>
    <BODY>
        <H1>The Guest Book</H1>
        Here are the current entries in the file: 
        <BR>
        <BR>
        <%
            String file = application.getRealPath("/") + "basic.jsp";
            File fileObject = new File(file);
            char data[] = new char[(int) fileObject.length()];
            FileReader filereader = new FileReader(file);
            int charsread = filereader.read(data);
            out.println(new String(data, 0 , charsread));
            filereader.close();
        %>
    </BODY>
</HTML>





Reading Binary Data

<%@ page import="java.io.*" %>
<HTML>
    <HEAD>
        <TITLE>Reading Binary Data</TITLE>
    </HEAD>
    <BODY>
        <H1>Reading Binary Data</H1>
        This page reads binary data from a file.
        <BR>
        Read this data:
        <BR>
        <BR>
        <%
            String file = application.getRealPath("/") + "test.dat";
            
            double data[] = {1.234, 2.345, 3.456, 4.567};
            FileOutputStream fileoutputstream = new FileOutputStream(file);
            ObjectOutputStream objectStream = new ObjectOutputStream(fileoutputstream );
            for (int i = 0; i < data.length; i++) {
                objectStream.writeDouble(data[i]);
            }
            objectStream.close();
            
            
            FileInputStream fileinputstream = new FileInputStream(file);
            ObjectInputStream inputobjectStream = new ObjectInputStream(fileinputstream); 
            for(int i = 0; i < 4; i++){
                double d = inputobjectStream.readDouble();
                out.println(d);
            }
            objectStream.close();
        %>
    </BODY>
</HTML>





Save Form Input to File using JSP

index.jsp



<HTML>
    <HEAD>
        <TITLE>Please Sign My Guest Book!</TITLE>
    </HEAD>
    <BODY>
        <CENTER>
            <H1>Please Sign My Guest Book!</H1>
            <FORM ACTION="basic.jsp" METHOD="POST">
                Your name:
                <INPUT TYPE="TEXT" NAME="TEXT1">
                <BR>
                <BR>
                <BR>
                Your comments:
                <BR>
                <TEXTAREA NAME="TEXTAREA1" ROWS="5" COLS="50"></TEXTAREA>
                <BR>
                <INPUT TYPE="SUBMIT" VALUE="Submit"><INPUT TYPE="RESET" VALUE="Reset">
            </FORM>
        </CENTER>
    </BODY>
</HTML>





Writing Binary Data

<%@ page import="java.io.*" %>
<HTML>
    <HEAD>
        <TITLE>Writing Binary Data</TITLE>
    </HEAD>
    <BODY>
        <H1>Writing Binary Data</H1>
        This page writes binary data to a file.
        <BR>
        <%
            byte data[] = {1, 2, 3, 4};
            String file = application.getRealPath("/") + "test.dat";
            FileOutputStream fileoutputstream = new FileOutputStream(file);
            for (int i = 0; i < data.length; i++) {
                fileoutputstream.write(data[i]);
            }
            fileoutputstream.close();
        %>
        Four byte values were written to a file byte by byte.
    </BODY>
</HTML>