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(); } }

弘速云香港VPSVPS线路有CN2+BGP、CN2 GIA,KVM虚拟化架构,裸金属月付564元

弘速云怎么样?弘速云是创建于2021年的品牌,运营该品牌的公司HOSU LIMITED(中文名称弘速科技有限公司)公司成立于2021年国内公司注册于2019年。HOSU LIMITED主要从事出售香港vps、美国VPS、香港独立服务器、香港站群服务器等,目前在售VPS线路有CN2+BGP、CN2 GIA,该公司旗下产品均采用KVM虚拟化架构。可联系商家代安装iso系统。点击进入:弘速云官方网站地址...

轻云互联-618钜惠秒杀,香港CN2大宽带KVM架构云服务器月付22元,美国圣何塞精品云月付19元爆款!海量产品好货超值促销进行中!

官方网站:点击访问青云互联活动官网优惠码:终身88折扣优惠码:WN789-2021香港测试IP:154.196.254美国测试IP:243.164.1活动方案:用户购买任意全区域云服务器月付以上享受免费更换IP服务;限美国区域云服务器凡是购买均可以提交工单定制天机防火墙高防御保护端口以及保护模式;香港区域购买季度、半年付、年付周期均可免费申请额外1IP;使用优惠码购买后续费周期终身同活动价,价格不...

野草云99元/月 ,香港独立服务器 E3-1230v2 16G 30M 299元/月 香港云服务器 4核 8G

野草云月末准备了一些促销,主推独立服务器,也有部分云服务器,价格比较有性价比,佣金是10%循环,如果有时间请帮我们推推,感谢!公司名:LucidaCloud Limited官方网站:https://www.yecaoyun.com/香港独立服务器:CPU型号内存硬盘带宽价格购买地址E3-1230v216G240GB SSD或1TB 企盘30M299元/月点击购买E5-265016G240GB SS...

java读写文件为你推荐
soapui下载手机系统用户界面软件下载策略组怎样打开组策略???溢出隐藏关于CSS样式的问题,怎么可以让div的内容自动换行,溢出隐藏,还要加省略号?arc是什么意思数学中的arctan是什么意思保留两位有效数字什么叫保留两位有效数字vipjr大家觉得vipjr少儿英语怎么样?靠谱不flowplayer如何编译flowplayer源码弹幕网站谁能给我解释一下什么叫A站,什么叫B站?都是啥网站?数据分析报告范文如何做一个好的数据分析报告图片存储手机照片的保存方法?
qq空间域名 域名交易网 net主机 西安服务器 a2hosting 荷兰服务器 burstnet ix主机 BWH gateone 云鼎网络 100m免费空间 165邮箱 seednet hostloc 阿里校园 台湾谷歌 优酷黄金会员账号共享 环聊 域名与空间 更多