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!!!

wordpress通用企业主题 wordpress高级企业自适应主题

wordpress高级企业自适应主题,通用型企业展示平台 + 流行宽屏设计,自适应PC+移动端屏幕设备,完美企业站功能体验+高效的自定义设置平台。一套完美自适应多终端移动屏幕设备的WordPress高级企业自适应主题, 主题设置模块包括:基本设置、首页设置、社会化网络设置、底部设置、SEO设置; 可以自定义设置网站通用功能模块、相关栏目、在线客服及更多网站功能。点击进入:wordpress高级企业...

ucloud国内云服务器2元/月起;香港云服务器4元/首月;台湾云服务器3元/首月

ucloud云服务器怎么样?ucloud为了扩大云服务器市场份额,给出了超低价云服务器的促销活动,活动仍然是此前的Ucloud全球大促活动页面。目前,ucloud国内云服务器2元/月起;香港云服务器4元/首月;台湾云服务器3元/首月。相当于2-4元就可以试用国内、中国香港、中国台湾这三个地域的云服务器1个月了。ucloud全球大促仅限新用户,国内云服务器个人用户低至56元/年起,香港云服务器也仅8...

妮妮云香港CTG云服务器1核 1G 3M19元/月

香港ctg云服务器香港ctg云服务器官网链接 点击进入妮妮云官网优惠活动 香港CTG云服务器地区CPU内存硬盘带宽IP价格购买地址香港1核1G20G3M5个19元/月点击购买香港2核2G30G5M10个40元/月点击购买香港2核2G40G5M20个450元/月点击购买香港4核4G50G6M30个80元/月点击购买香...

java读写文件为你推荐
混淆矩阵非监督分类可以用混淆矩阵进行精度评价吗md5值文件名后缀为MD5是什么文件。oracle索引oracle表加索引有什么用? 请举一个例子说明谢谢电子日历怎样调整电子日历上的时间deviceidAndroid里DeviceId和AndroidId都是什么意思?jqlJQL JINQILIN注册过商标吗?还有哪些分类可以注册?调度系统1.说明高级调度、中级调度和低级调度的基本含义。数秦科技安徽数智通科技有限公司怎么样?flowplayer如何编译flowplayer源码faq是什么意思在国际贸易中“FAQ”代表的是什么意思
域名解析文件 香港加速器 cve-2014-6271 iis安装教程 百兆独享 php空间购买 免费美国空间 idc查询 网页提速 阿里云官方网站 smtp服务器地址 云服务器比较 可外链的相册 godaddy空间 腾讯网盘 fatcow 百度新闻源申请 e-mail linux服务器系统 alexa搜 更多