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

Spinservers美国圣何塞服务器$111/月流量10TB

Spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立服务器租用和Hybrid Dedicated等,数据中心位于美国德克萨斯州达拉斯和加利福尼亚圣何塞机房。TheServerStore.com,自 1994 年以来,它是一家成熟的企业 IT 设备供应商,专门从事二手服务器和工作站业务,在德克萨斯州拥有 40,000 平方英尺的仓库,库存中始终有...

ReliableSite:美国服务器租用,洛杉矶/纽约/迈阿密等机房;E3-1240V6/64GB/1TSSD,$95/月

reliablesite怎么样?reliablesite是一家于2006年成立的老牌美国主机商,主要提供独服,数据中心有迈阿密、纽约、洛杉矶等,均免费提供20Gbps DDoS防护,150TB月流量,1Gbps带宽。月付19美金可升级为10Gbps带宽。洛杉矶/纽约/迈阿密等机房,E3-1240V6/64GB内存/1TB SSD硬盘/DDOS/150TB流量/1Gbps带宽/DDOS,$95/月,...

Stablehost 美国主机商黑五虚拟主机四折

如今我们网友可能较多的会选择云服务器、VPS主机,对于虚拟主机的话可能很多人不会选择。但是我们有些外贸业务用途的建站项目还是会有选择虚拟主机的。今天看到的Stablehost 商家虚拟主机在黑五期间也有四折优惠,对于这个服务商而言不是特别的喜欢,虽然他们商家和我们熟悉的老鹰主机商有些类似,且在后来老鹰主机改版和方案后,Stablehost 商家也会跟随改版,但是性价比认为不如老鹰主机。这次黑色星期...

java读写文件为你推荐
显卡挖矿啥意思原因竟为了用来挖矿,躺着赚钱,显卡挖矿什么意思rbf神经网络RBF神经网络和BP神经网络有什么区别google邮箱注册怎样创建google邮箱策略组简述组策略的概念 急急急bindservice安卓里 remote service到底有什么用李昊天铠甲勇士2刑天的李昊天秘密是什么李昊天李昊天这名字给多少分tvosTVOS推广怎么样?jql建筑设计图纸上JQL 梁,是不是地下正负零基础梁?急急!ruby语言公司实习让我学习RUBY语言,不知道RUBY语言发展前景怎么样,值不值的去学习。
美国vps推荐 冰山互联 arvixe oneasiahost hawkhost优惠码 Vultr payoneer unsplash realvnc 创宇云 tightvnc 好看的桌面背景图 网站被封 web服务器架设 isp服务商 能外链的相册 shopex主机 网站在线扫描 四川电信商城 smtp虚拟服务器 更多