作者共发了1篇帖子。 内容转换:不转换▼
 
点击 回复
350 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)
 

本帖信息

点击数:350 回复数:0
评论数: ?
作者:巨大八爪鱼
最后回复:巨大八爪鱼
最后回复时间:2016-9-29 21:01
 
©2010-2024 Arslanbar Ver2.0
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。