初学:读写文本文件....

xkent77 2003-10-15 05:19:15
这样写,出错...请问大家是如何用JAVA读写文本文件的?

读文件:
File file_tem = new File (filepath);
if(file_tem.exists())
{
FileReader fr=new FileReader(filepath);
String out_temp;
if(fr.read(out_temp)==1)
{
System.out.println("The content of the file:");
System.out.println(out_temp);
}
else
{
System.out.println("Error");
}
fr.close();
}
return true;
}

写文件:
File file_tem = new File (filepath);
if(file_tem.exists())
{
FileWriter fw=new FileWriter(filepath);
String out_temp="写入文件的内容";
fw.write(out_temp);
fr.close();
}
return true;
}

请教......TKS...
...全文
39 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xkent77 2003-10-20
  • 打赏
  • 举报
回复
FileReader.read()不能将一个文件的内容读出至一个字符串吗?

FileWriter.write()不能将一个字符串直接写入到文件中吗?

要是一个个BYTE写,不是很方便吧..

我看书说:文本文件读写用FileReader/FileWriter....
BYTE文件读写用InputStream/OutputStream.....

对吗??

(初学JAVA,找不到北了)
fuzhan820 2003-10-17
  • 打赏
  • 举报
回复
学习
qixin_lover 2003-10-17
  • 打赏
  • 举报
回复
//package fileutilities;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
//import dbutilities.*;

/*
对文件进行操作
FileReader 每次读取的是一个字符charactor,FileInputStram 每次读取的是一个字节byte
FileReader.read()返回一个int,其取值范围是0 到65535,通常来说是两个字节的;FileWriter.write(int c)
向文件写入一个int,通常来说也是两个字节的,如果某个字符的高位字节为空,那么其高位字节将被舍弃;
FileOutputStream.write(int b)虽然接受一个int作为参数,实际上只向文件写入一个字节,如果传递过来
的参数是一个双字节的汉字,其高位字节将被舍弃,造成文件内容错误。
建议:永远只使用InputStream/OutputStream进行IO操作。

*/
public class fileoperation {

File currentfile;
File currentfile2;
FileReader infile;
FileWriter outfile;
String path=null;

public void deletefile(String delfile) //删除文件,参数是包含绝对路径的文件名
{
try {
//this.path = dir;
currentfile = new File(delfile);
currentfile.delete();
}
catch(Exception ex) { System.out.print(ex); }

}

public void renamefile(String filepath,String sfile,String dfile)//将文件名为sfile的文件更名为dfile
{
try {
this.path = filepath;
currentfile = new File(path+sfile);
currentfile2 = new File(path+dfile);
currentfile.renameTo(currentfile2);
}
catch(Exception ex) { System.out.print(ex); }
}

public void copyfile(String filepath,String sfile,String dfile)//将文件名为sfile的文件进行拷贝,目标文件为dfile
{
try {
this.path = filepath;
currentfile = new File(path+sfile);
currentfile2 = new File(path+dfile);

infile = new FileReader(currentfile);
outfile = new FileWriter(currentfile2);

int c;
while((c = infile.read()) != -1)
{
outfile.write(c);
}

infile.close();
outfile.close();

}
catch(Exception ex){ System.out.print(ex); }
}

public static void main(String args[])
{
fileoperation fileop = new fileoperation();
String filepath="D:/fxbg/email/newmail/upload/";
System.out.println("File Name:");
String soufile="x1.txt";
String desfile="x2.txt";
fileop.copyfile(filepath,soufile,desfile);
}

}
xkent77 2003-10-16
  • 打赏
  • 举报
回复
有人教我吗?

         针对有大量Unity初学者想在短期内(例如:2-4周时间)快速掌握Untiy的基本使用,了解基本开发技能。为满足入门学员的学习要求,“刘国柱讲Unity”系列课程,因此推出了本套“Unity快速入门系列课程”,目前内容包含如下:    1: 项目“我的世界”: 讲解Unity软件的重要组成窗口与基本使用。    2: 项目:  台球游戏:   讲解Untiy中脚本的基本使用,Unity碰撞体与触发器的使用。    3:  项目: “Flappy Bird” 讲解纯2D(手游)游戏的开发过程,了解Unity2D 开发技能。    4:  项目: 太空射击 讲解使用3D空间,开发2D手游的过程,其中讲解“单例模式”做数据传值技术、基本粒子系统的使用、音频处理方法、碰撞与触发检测脚本算法......    5:   模块“移动端发布技术”,讲解快速发布Android 发布包(*.APK文件)技术。讲解JDK的安装与配置,以及Android SDK 的配置方式方法。    一、热更新系列(技术含量:中高级):A:《lua热更新技术中级篇》https://edu.csdn.net/course/detail/27087B:《热更新框架设计之Xlua基础视频课程》https://edu.csdn.net/course/detail/27110C:《热更新框架设计之热更流程与热补丁技术》https://edu.csdn.net/course/detail/27118D:《热更新框架设计之客户端热更框架(上)》https://edu.csdn.net/course/detail/27132E:《热更新框架设计之客户端热更框架(中)》https://edu.csdn.net/course/detail/27135F:《热更新框架设计之客户端热更框架(下)》https://edu.csdn.net/course/detail/27136二:框架设计系列(技术含量:中级): A:《游戏UI界面框架设计系列视频课程》https://edu.csdn.net/course/detail/27142B:《Unity客户端框架设计PureMVC篇视频课程(上)》https://edu.csdn.net/course/detail/27172C:《Unity客户端框架设计PureMVC篇视频课程(下)》https://edu.csdn.net/course/detail/27173D:《AssetBundle框架设计_框架篇视频课程》https://edu.csdn.net/course/detail/27169三、Unity脚本从入门到精通(技术含量:初级)A:《C# For Unity系列之入门篇》https://edu.csdn.net/course/detail/4560B:《C# For Unity系列之基础篇》https://edu.csdn.net/course/detail/4595C: 《C# For Unity系列之中级篇》https://edu.csdn.net/course/detail/24422D:《C# For Unity系列之进阶篇》https://edu.csdn.net/course/detail/24465四、虚拟现实(VR)与增强现实(AR):(技术含量:初级)A:《虚拟现实之汽车仿真模拟系统 》https://edu.csdn.net/course/detail/26618五、Unity基础课程系列(技术含量:初级) A:《台球游戏与FlappyBirds—Unity快速入门系列视频课程(第1部)》 https://edu.csdn.net/course/detail/24643B:《太空射击与移动端发布技术-Unity快速入门系列视频课程(第2部)》https://edu.csdn.net/course/detail/24645 C:《Unity ECS(二) 小试牛刀》https://edu.csdn.net/course/detail/27096六、Unity ARPG课程(技术含量:初中级):A:《MMOARPG地下守护神_单机版实战视频课程(上部)》https://edu.csdn.net/course/detail/24965B:《MMOARPG地下守护神_单机版实战视频课程(中部)》https://edu.csdn.net/course/detail/24968C:《MMOARPG地下守护神_单机版实战视频课程(下部)》https://edu.csdn.net/course/detail/24979

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧