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

ThomasHost(月付5美元)美国/法国/英国/加拿大KVM,支持Windows

ThomasHost域名注册自2012年,部落最早分享始于2016年,还算成立了有几年了,商家提供基于KVM架构的VPS,数据中心包括美国、法国、英国、加拿大和爱尔兰等6个地区机房,VPS主机套餐最低2GB内存起步,支持Windows或者Linux操作系统,1Gbps端口不限制流量。最近商家提供了一个5折优惠码,优惠后最低套餐月付5美元起。下面列出部分套餐配置信息。CPU:1core内存:2GB硬...

杭州王小玉网-美国CERA 2核8G内存19.9元/月,香港,日本E3/16G/20M CN2带宽150元/月,美国宿主机1500元,国内宿主机1200元

官方网站:点击访问王小玉网络官网活动方案:买美国云服务器就选MF.0220.CN 实力 强 强 强!!!杭州王小玉网络 旗下 魔方资源池 “我亏本你引流活动 ” mf.0220.CNCPU型号内存硬盘美国CERA机房 E5 2696v2 2核心8G30G总硬盘1个独立IP19.9元/月 续费同价mf.0220.CN 购买湖北100G防御 E5 2690v2 4核心4G...

丽萨主机122元/每季,原生IP,CN2 GIA网络

萨主机(lisahost)新上了美国cn2 gia国际精品网络 – 精品线路,支持解锁美区Netflix所有资源,HULU, DISNEY, StartZ, HBO MAX,ESPN, Amazon Prime Video等,同时支持Tiktok。套餐原价基础上加价20元可更换23段美国原生ip。支持Tiktok。成功下单后,在线充值相应差价,提交工单更换美国原生IP。!!!注意是加价20换原生I...

java读写文件为你推荐
ipv6无网络访问权限WIN7本地连接,IPV4和IPV6无网络访问权限怎么解决akf德州水份检测仪价格,AKF系列卡尔费休水份测定仪和世界顶级进口品牌相比怎么样?weakhashmapJava中isEmpty方法如何使用?oracle索引如何在ORACLE数据库的字段上建立索引网关和路由器的区别网关和路由器有什么区别拓扑关系什么是空间数据的拓扑关系调度系统1.说明高级调度、中级调度和低级调度的基本含义。inode智能客户端inode智能客户端怎么使用wifi?layoutsubviews如何修改TableViewCell中的ImageView的Frame和大小什么是SOA什么是中子刀?您了解吗?
最便宜的vps 最新代理服务器ip 如何申请免费域名 西安服务器 kdata 国内永久免费云服务器 mediafire下载 paypal认证 softbank官网 godaddy续费优惠码 英语简历模板word 租空间 发包服务器 免费个人空间申请 godaddy域名证书 qq对话框 如何安装服务器系统 根服务器 空间租赁 宏讯 更多