没有合适的资源?快使用搜索试试~ 我知道了~
java对各种文件操作.txt
3星 · 超过75%的资源 需积分: 9 24 下载量 114 浏览量
2008-12-28
16:19:42
上传
评论 1
收藏 9KB TXT 举报
温馨提示
试读
11页
java java对文件的操作 java操作文件 java java对文件的操作 java操作文件
资源推荐
资源详情
资源评论
java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。
新建目录<%@ page contentType="text/html;charset=gb2312"%>
<%
//String URL = request.getRequestURI();
String filePath="C:\\测试\\";
filePath=filePath.toString();//中文转换
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
%>
新建文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%
String filePath="c:/测试/newFile.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String content ="这是测试数据";
String strContent = content.toString();
myFile.println(strContent);
resultFile.close();
%>
删除文件<%@ page contentType="text/html;charset=gb2312"%>
新建目录<%@ page contentType="text/html;charset=gb2312"%>
<%
//String URL = request.getRequestURI();
String filePath="C:\\测试\\";
filePath=filePath.toString();//中文转换
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
%>
新建文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%
String filePath="c:/测试/newFile.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String content ="这是测试数据";
String strContent = content.toString();
myFile.println(strContent);
resultFile.close();
%>
删除文件<%@ page contentType="text/html;charset=gb2312"%>
<%
String filePath="c://测试//newFile.txt";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
if(myDelFile.exists())
{
myDelFile.delete();
out.println(filePath+"删除成功!!!");
}
else
{
out.println(filePath+"该文件不存在");
}
%>
文件拷贝<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
int bytesum=0;
int byteread=0;
//file:读到流中
InputStream inStream=new FileInputStream("c://测试//newFile.txt");
FileOutputStream fs=new FileOutputStream( "c://测试//copyFile.txt");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
out.println(bytesum);
fs.write(buffer,0,byteread);
String filePath="c://测试//newFile.txt";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
if(myDelFile.exists())
{
myDelFile.delete();
out.println(filePath+"删除成功!!!");
}
else
{
out.println(filePath+"该文件不存在");
}
%>
文件拷贝<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
int bytesum=0;
int byteread=0;
//file:读到流中
InputStream inStream=new FileInputStream("c://测试//newFile.txt");
FileOutputStream fs=new FileOutputStream( "c://测试//copyFile.txt");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
out.println(bytesum);
fs.write(buffer,0,byteread);
剩余10页未读,继续阅读
资源评论
- l4942292202012-09-21文档教了基本的java对文件的操作
yangfeihu
- 粉丝: 17
- 资源: 94
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功