目前共有1篇帖子。 內容轉換:不轉換▼
 
點擊 回復
342 0
【程序】Java读写文本文件
一派護法 十九級
1樓 發表于:2016-9-29 21:01
<%@ page import="java.io.*" pageEncoding="utf-8" %><%!
// 数组清零
void empty(byte[] buffer) {
    for (int i = 0; i < buffer.length; i++) {
        buffer[i] = 0;
    }
}
%><!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>File Operations</title>
<style>
body {
    line-height: 1.4em; /* 设置行高 */
}
</style>
</head>

<body>
<%
File file = new File("F:/My Data/2015/October/HuangShiqiu.txt");
File file2 = new File("F:/My Data/2016/September/java_copied_file.txt");
FileInputStream rf = new FileInputStream(file);
FileOutputStream wf = new FileOutputStream(file2);
byte[] buffer = new byte[10];
int length;
out.println("<strong>文件的内容为: </strong><br>");
while ((length = rf.read(buffer)) != -1) { // 若length=-1, 则文件已读取完毕
    String str = new String(buffer);
    str = str.replace("\n", "<br>");
    out.print(str);
    
    wf.write(buffer, 0, length); // 写入文件
    empty(buffer);
}
rf.close();
wf.close();
out.println("<br><strong>文件已复制</strong>");
%></body>
</html>

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:342 回複數:0
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-9-29 21:01
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。