java读写文件java文件读写的文件应该放哪个路径

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

java如何读取一个txt文件的所有内容

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class ReadFile { public static void main(String[] args) throws IOException { String fileContent = readFileContent(""); System.out.println(fileContent); } //参数string为你的文件名 private static String readFileContent(String fileName) throws IOException { File file = new File(fileName); BufferedReader bf = new BufferedReader(new FileReader(file)); String content = ""; StringBuilder sb = new StringBuilder(); while(content != null){ content = bf.readLine(); if(content == null){ break; } sb.append(content.trim()); } bf.close(); return sb.toString(); } } 求采纳为满意回答。

java,编写一个程序,可以读取文件数据

package?dome.myword.test; import?java.io.BufferedInputStream; import?java.io.BufferedReader; import?java.io.FileInputStream; import?java.io.FileNotFoundException; import?java.io.FileOutputStream; import?java.io.FileReader; import?java.io.IOException; public?class?Myword?{ public?static?void?main(String[]?args)?throws?IOException?{ try?{ FileInputStream?file?=?new?FileInputStream("e:/myText.txt"); BufferedInputStream?Bfile?=?new?BufferedInputStream(file); byte[]?b?=?new?byte[1024]; String?s?=?""; int?bytesRead=0;?? while((bytesRead=Bfile.read(b))!=-1){ s+=?new?String(b,0,bytesRead); } System.out.println(s); String[]?words?=?s.split("?"); int?sum?=?words.length; int?max?=?words[0].length(); ?????? ????????String?maxWord?=?""; ????????int?avgs?=?0; ????????for?(int?i?=?1;?i?<?words.length;?i++)?{ ???????????? ????????????if(words[i].length()>max)?{??????????????? ???????????????max?=?Math.max(max,?words[i].length()); ??????????????? ???????????????maxWord?=?words[i]; ????????????} ????????????avgs?=?avgs+words[i].length()+words[0].length(); ????????} ?? ????????String?path?=?"e:/myText.txt"?; ????????FileReader?fr?=?new?FileReader(path);? ????????BufferedReader?br?=?new?BufferedReader(fr); ???????? int?x?=?0; ???????? while(br.readLine()?!=?null)?{? ???????? x++;? ???????? } ? ????????System.out.println("总行数"+x); ????????int?avg?=?avgs/sum; ????????System.out.println("平均长度:"+avg); ????????System.out.println("最长单词:"+maxWord); System.out.println("单词总数:"+sum); Bfile.close(); ?String?fileName?=?"e:/mytextdata.txt"; ?FileOutputStream?out?=?new?FileOutputStream(fileName); ?String?str?="单词总数:"+sum+" "+?"总行数:"+x+" "+"最长单词:"+maxWord+" "+"平均长度:"+avg; ?out.write(str.getBytes()); ?out.close(); System.out.println("输出文本完毕"); }?catch?(FileNotFoundException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } } }有问题再叫我。

下面是测试结果

java 在文件里面读写数据

亲,我也在写这题目,我按你的方法写实现了 Scanner input = new Scanner("Exercise9_19.txt");这句有问题。

将"Exercise9_19.txt"改为你前面定义的File变量sourceFile才能正确读出。

正确写法:Scanner input = new Scanner(sourceFile); 不然你报的异常为java.util.InputMismatchException期望参数不匹配异常。

java文件读写的文件应该放哪个路径

放文件所在路径啊,比如我的文件在C盘的aa文件夹下的bb.txt File f=new File("C://aa/bb.txt"); InputStreamReader isr=new InputStreamReader(f); BufferedReader buf=new BufferedReader(isr); String s=buf.readLine(); OK!!!

Hostigger不限流量VPS年20美元

Hostigger 主机商在前面的文章中也有介绍过几次,这个商家运营时间是有一些年份,只不过在我们圈内好像之前出现的次数不多。最近这段时间商家有提供不限流量的VPS主机,逐渐的慢慢被人认识到。在前面的介绍到他们提供的机房还是比较多的,比如土耳其、美国等。今天看到Hostigger 商家居然改动挺大的,原来蛮好的域名居然这次连带官方域名都更换掉去掉一个G(Hostiger )。估摸着这个域名也是之前...

friendhosting:(优惠55%)大促销,全场VPS降价55%,9个机房,不限流量

每年的7月的最后一个周五是全球性质的“系统管理员日”,据说是为了感谢系统管理员的辛苦工作....friendhosting决定从现在开始一直到9月8日对其全球9个数据中心的VPS进行4.5折(优惠55%)大促销。所有VPS基于KVM虚拟,给100M带宽,不限制流量,允许自定义上传ISO...官方网站:https://friendhosting.net比特币、信用卡、PayPal、支付宝、微信、we...

优林70/月,西南高防地区最低70/月

优林怎么样?优林好不好?优林 是一家国人VPS主机商,成立于2016年,主营国内外服务器产品。云服务器基于hyper-v和kvm虚拟架构,国内速度还不错。今天优林给我们带来促销的是国内西南地区高防云服务器!全部是独享带宽!续费同价!官方网站:https://www.idc857.com​地区CPU内存硬盘流量带宽防御价格购买地址德阳高防4核4g50G无限流量10M100G70元/月点击购买德阳高防...

java读写文件为你推荐
deepfakes什么是DeepFreeze ? 怎么使用啊?oncontextmenu鼠标右键很好用,但是左键一点反应也没有,请问是什么原因呢?oracle索引Oracle中有多少种索引类型webcrackwebcrack4.0bindservice安卓里 remote service到底有什么用inode智能客户端我的电脑上inode智能客户端连接网络时,提示~服务器没有响应,请确认当前认证网卡已连接到合适的网问卷星登陆问卷星的使用步骤社工库网站怎么找社工库?medias请帮我详细解释一下 chronological order和in medias res12306注册12306网站账户注册
免费国内空间 免费vps 查询ip地址 动态域名解析软件 国外空间服务商 sockscap 2017年万圣节 警告本网站美国保护 怎么测试下载速度 双11秒杀 域名和空间 美国网站服务器 万网主机管理 dnspod 监控服务器 新加坡空间 徐州电信 magento主机 easypanel 卡巴斯基免费下载 更多