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

Sharktech:无限流量服务器丹佛,洛杉矶,荷兰$49/月起,1Gbps带宽哦!

鲨鱼机房(Sharktech)我们也叫它SK机房,是一家成立于2003年的老牌国外主机商,提供的产品包括独立服务器租用、VPS主机等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等,主打高防产品,独立服务器免费提供60Gbps/48Mpps攻击防御。机房提供1-10Gbps带宽不限流量服务器,最低丹佛/荷兰机房每月49美元起,洛杉矶机房最低59美元/月起。下面列出部分促销机型的配置信息。机房...

企鹅小屋6折年付240元起,美国CN2 GIA VPS促销,独享CPU,三网回程CN2 GIA

企鹅小屋怎么样?企鹅小屋最近针对自己的美国cn2 gia套餐推出了2个优惠码:月付7折和年付6折,独享CPU,100%性能,三网回程CN2 GIA网络,100Mbps峰值带宽,用完优惠码1G内存套餐是年付240元,线路方面三网回程CN2 GIA。如果新购IP不能正常使用,请在开通时间60分钟内工单VPS技术部门更换正常IP;特价主机不支持退款。点击进入:企鹅小屋官网地址企鹅小屋优惠码:年付6折优惠...

MineServer:香港CMI/洛杉矶GIA VPS,2核/2GB内存/20GB NVME/3.5TB流量/200Mbps/KVM,288元/年

mineserver怎么样?mineserver是一家国人商家,主要提供香港CN2 KVM VPS、香港CMI KVM VPS、日本CN2 KVM VPS、洛杉矶cn2 gia端口转发等服务,云服务器网(yuntue.com)介绍过几次,最近比较活跃。现在新推出了3款特价KVM VPS,性价比高,香港CMI/洛杉矶GIA VPS,2核/2GB内存/20GB NVME/3.5TB流量/200Mbps...

java读写文件为你推荐
策略组组策略完全使用方法mindmanager破解版谁有MindManager破解版的保留两位有效数字物理中保留两位有效数字是保留小数点后的两位还是从小数点前不是0的数开始保留两位?spawningVC中Error spawning cl.exe错误的解决方法.radius认证PPPoE有认证的功能,RADIUS也有验证功能,两者有区别么??色库石伟伟怎么写啊什么是句柄按键精灵的句柄是什么?toolstripWinOperationClass是什么来的,什么用思科网络工程师考完思科认证网络工程师干什么?科来网络分析科来网络分析系统的安装部署
vps安全设置 xenvps 域名服务器上存放着internet主机的 bash漏洞 表单样式 100x100头像 韩国名字大全 河南m值兑换 idc是什么 中国电信宽带测速网 t云 卡巴斯基是免费的吗 华为k3 葫芦机 hdroad 石家庄服务器 第八届中美互联网论坛 linux服务器系统 西部数码主机 bwg 更多