?? byte_write_file.jsp
字號:
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import="java.io.*" %>
<%
//獲取站點目錄的絕對路徑
String sPath=request.getRealPath("/");
//所要打開的文件名
String sFileName1 = "test.txt";
//所要寫入內容的文件名
String sFileName2 = "new.txt";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>順序寫文件內容</title>
</head>
<body>
<h1 align="center">將文件<%=sFileName1%>的內容寫入文件<%=sFileName2%></h1>
<%
//創建所要打開文件對象
File fObj1 = new File(sPath+sFileName1);
//創建文件輸入流對象
FileInputStream fins = new FileInputStream(fObj1);
//獲取可以讀入的字節數目
int n;
n = fins.available();
//從輸入流中讀取數據到字節數組
byte bTemp[] = new byte[n];
int x = fins.read(bTemp);
//創建所要輸入內容的文件對象
File fObj2 = new File(sPath+sFileName2);
//創建一個空文件
fObj2.createNewFile();
//創建文件輸出流對象
FileOutputStream fouts = new FileOutputStream(fObj2);
//將內容寫入到文件中
fouts.write(bTemp);
//關閉輸入流和輸出流
fins.close();
fouts.close();
//創建文件對象
File fObj3 = new File(sPath+sFileName2);
//創建文件輸入流對象
FileInputStream fins1 = new FileInputStream(fObj3);
//獲取可以讀入的字節數目
n = fins1.available();
x = fins1.read(bTemp);
//將字節數組轉換成字符串
String sCon = new String(bTemp,0);
out.println(sFileName2+"的文件內容如下:<br>"+sCon);
//關閉輸入流
fins1.close();
%>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -