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

10gbiz($2.36/月),香港/洛杉矶CN2 GIA线路VPS,香港/日本独立服务器

10gbiz发布了9月优惠方案,针对VPS、独立服务器、站群服务器、高防服务器等均提供了一系列优惠方面,其中香港/洛杉矶CN2 GIA线路VPS主机4折优惠继续,优惠后最低每月仅2.36美元起;日本/香港独立服务器提供特价款首月1.5折27.43美元起;站群/G口服务器首月半价,高防服务器永久8.5折等。这是一家成立于2020年的主机商,提供包括独立服务器租用和VPS主机等产品,数据中心包括美国洛...

易探云:香港CN2云服务器低至18元/月起,183.60元/年

易探云怎么样?易探云最早是主攻香港云服务器的品牌商家,由于之前香港云服务器性价比高、稳定性不错获得了不少用户的支持。易探云推出大量香港云服务器,采用BGP、CN2线路,机房有香港九龙、香港新界、香港沙田、香港葵湾等,香港1核1G低至18元/月,183.60元/年,老站长建站推荐香港2核4G5M+10G数据盘仅799元/年,性价比超强,关键是延迟全球为50ms左右,适合国内境外外贸行业网站等,如果需...

iON Cloud:新加坡cn2 gia vps/1核/2G内存/25G SSD/250G流量/10M带宽,$35/月

iON Cloud怎么样?iON Cloud升级了新加坡CN2 VPS的带宽和流量最低配的原先带宽5M现在升级为10M,流量也从原先的150G升级为250G。注意,流量也仅计算出站方向。iON Cloud是Krypt旗下的云服务器品牌,成立于2019年,是美国老牌机房(1998~)krypt旗下的VPS云服务器品牌,主打国外VPS云服务器业务,均采用KVM架构,整体性能配置较高,云服务器产品质量靠...

java读写文件为你推荐
onboardon board是什么意思?元宝网vap数字资产是不是传销rbf神经网络MATLAB工具箱里的RBF神经网络newrb是什么算法showwindowVB6.0中ShowWindow函数的使用天融信防火墙如何使用天融信NGFW4000防火墙工具layoutsubviews如何让NSSplitView的子View的大小固定水表读数家用水表怎么读数文件系统类型windows 操作系统的文件系统具体有哪几种思科网络工程师cisco网络工程师clamwin比较几款杀毒软件(AVG Avira ClamWin)
海外服务器租用 域名主机 万网域名查询 北京域名注册 日本私人vps xenvps 域名服务器是什么 二级域名申请 快速域名备案 cn域名备案 什么是域名解析 cdn服务器 免备案cdn 免费名片模板 免费smtp服务器 本网站服务器在美国 qq云端 百度云1t 国外视频网站有哪些 腾讯总部在哪 更多