java读写文件如何用java读取txt文件

java读写文件  时间:2021-07-13  阅读:()

java读写文件

很简单,用java的IO流的DataOutputStream 中的writeInt方法就可以,读就用DataInputStream 中的readInt方法就可以了 ======================== 是你自己不明白你的需求吧,byte类型是什么,你知道吗?如果非要用int转byte,取出再转也可以,DataInputStream也有writeByte方法,但世界没有任何一个人这么做的,int类型存进,取出就是int,你却非得多此一举

java写入文件的几种方法

用构造方法 FileWriter fileWriter=new FileWriter("c:\Result.txt", true); // true代表追加 同理 BufferedWriter writer = new BufferedWriter(new FileWriter(new File("c:\Result.txt"), true));

java如何写入txt文件

import java.io.*; import java.util.*; public class LogTest{ public static void main(String[] args) { File log=new File("user\log\login.log"); String newLog="UserName:Jim Green | Date:"+new Date()+" | IP:202.65.21.14"; appendLog(log,newLog); } public static void appendLog(File log,String newLog) { Scanner sc=null; PrintWriter pw=null; try{ if(!log.exists())//如果文件不存在,则新建. { File parentDir=new File(log.getParent()); if(!parentDir.exists())//如果所在目录不存在,则新建. parentDir.mkdirs(); log.createNewFile(); } sc=new Scanner(log); StringBuilder sb=new StringBuilder(); while(sc.hasNextLine())//先读出旧文件内容,并暂存sb中; { sb.append(sc.nextLine()); sb.append(" ");//换行符作为间隔,扫描器读不出来,因此要自己添加. } sc.close(); pw=new PrintWriter(new FileWriter(log),true); /*A.*/pw.println(sb.toString());//,写入旧文件内容. /*B.*/pw.println(newLog);//写入新日志. /* * 如果先写入A,最近日志在文件最后. * 如是先写入B,最近日志在文件最前. */ pw.close(); } catch(IOException ex) { ex.printStackTrace(); } } }

java文件读写

在网上查了很多关于修改文件的方法,不得其要领。

自己想了两个取巧的办法,来解决对文件的修改。

一:读取一个文件file1(FileReader and BufferedReader),进行操作后写入file2(FileWriter and BufferedWriter),然后删除file1,更改file2文件名为file1(Rename()方法)。

二:创建字符缓冲流(StringBuffer),读取文件内容赋给字符缓冲流,再将字符缓冲流中的内容写入到读取的文件中。

例如: test.txt 这里是放在d盘的根目录下,内容如下 able adj 有才干的,能干的 active adj 主动的,活跃的 adaptable adj 适应性强的 adroit adj 灵巧的,机敏的 运行结果生成在同目录的 test1.txt中 able #adj*有才干的,能干的 active #adj*主动的,活跃的 adaptable #adj*适应性强的 adroit #adj*灵巧的,机敏的 代码: public class Test { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new FileReader("D:\test.txt")); StringBuffer sb = new StringBuffer(); String lineContent = null ;while( (lineContent = br.readLine()) != null){ String[] sp = lineContent.split(" ");sp[0] = sp[0].concat(" *");sp[1] = sp[1].concat("# ");for(int i=0;i sb.append(sp[i]);}sb.append(" ");}FileWriter fw = new FileWriter("D:\test2.txt"); fw.write(sb.toString()); br.close(); fw.close(); }}

如何用java读取txt文件

用java读取txt文件: public String read(String path) throws Exception { //读 File f = new File(path); FileInputStream input = new FileInputStream(f); BufferedInputStream buf=new BufferedInputStream(input); byte[] b=new byte[(int) f.length()]; input.read(b); input.close(); return new String(b); } public static void writeFileByByte(String path,String strs,boolean a) throws Exception{ //写 File f1=new File(path); FileOutputStream out=new FileOutputStream(f1,a); byte[] b=strs.getBytes(); out.write(b); out.close(); } 也可以参考JAVA IO。

public class ReadTxtFile { public static void main(String[] args) throws Exception { File file = new File("C:\Users\795829\\1.txt"); // 字符流读取文件数据 BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); System.exit(-1); // TODO 测试用 // 字节流读取文件数据 FileInputStream fis = new FileInputStream(file); int n = 1024; byte buffer[] = new byte[n]; while ((fis.read(buffer, 0, n) != -1) && (n > 0)) { System.out.print(new String(buffer)); } fis.close(); } }

RangCloud19.8元/月,香港cn2云主机,美国西雅图高防云主机28元/月起

rangcloud怎么样?rangcloud是去年年初开办的国人商家,RangCloud是一家以销售NAT起步,后续逐渐开始拓展到VPS及云主机业务,目前有中国香港、美国西雅图、韩国NAT、广州移动、江门移动、镇江BGP、山东联通、山东BGP等机房。目前,RangCloud提供香港CN2线路云服务器,电信走CN2、联通移动直连,云主机采用PCle固态硬盘,19.8元/月起,支持建站使用;美国高防云...

易探云香港云服务器价格多少钱1个月/1年?

易探云怎么样?易探云是目前国内少数优质的香港云服务器服务商家,目前推出多个香港机房的香港云服务器,有新界、九龙、沙田、葵湾等机房,还提供CN2、BGP及CN2三网直连香港云服务器。近年来,许多企业外贸出海会选择香港云服务器来部署自己的外贸网站,使得越来越多的用户会选择易探云作为网站服务提供平台。今天,云服务器网(yuntue.com)小编来谈谈易探云和易探云服务器怎么样?具体香港云服务器多少钱1个...

VPS云服务器GT线路,KVM虚vps消息CloudCone美国洛杉矶便宜年付VPS云服务器补货14美元/年

近日CloudCone发布了最新的补货消息,针对此前新年闪购年付便宜VPS云服务器计划方案进行了少量补货,KVM虚拟架构,美国洛杉矶CN2 GT线路,1Gbps带宽,最低3TB流量,仅需14美元/年,有需要国外便宜美国洛杉矶VPS云服务器的朋友可以尝试一下。CloudCone怎么样?CloudCone服务器好不好?CloudCone值不值得购买?CloudCone是一家成立于2017年的美国服务器...

java读写文件为你推荐
pexels视频素材网那个比较好,有好的正版高清视频素材网站推荐一下吗?scanf_sscanf_s和以前的scanf是一样等级的吗???gravatarWordPress头像无法显示怎么办图片地址怎么知道一张图片的地址西安论坛有没有谁被西安论坛活动忽悠过的啊?空白代码html空格代码怎么写rdlregardless是什么意思slideshare佳能复印MG3620怎么使用?调度系统1.说明高级调度、中级调度和低级调度的基本含义。欢迎页面怎样取消“欢迎页面”?
虚拟主机管理软件 免费域名注册网站 荣耀欧洲 wavecom idc评测网 国外私服 sub-process 密码泄露 网盘申请 英文站群 个人空间申请 ca4249 京东商城0元抢购 大容量存储器 秒杀汇 腾讯实名认证中心 国外代理服务器地址 河南移动网 网游服务器 服务器监测 更多