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:美国圣何塞服务器,双E5/64GB DDR4/2TB SSD/10Gbps端口月流量10TB,$111/月

spinservers怎么样?spinservers大硬盘服务器。Spinservers刚刚在美国圣何塞机房补货120台独立服务器,CPU都是双E5系列,64-512GB DDR4内存,超大SSD或NVMe存储,数量有限,机器都是预部署好的,下单即可上架,无需人工干预,有需要的朋友抓紧下单哦。Spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立...

月神科技:香港CN2/洛杉矶CN2/华中电信高防vps,月付20元起

月神科技怎么样?月神科技是由江西月神科技有限公司运营的一家自营云产品的IDC服务商,提供香港安畅、香港沙田、美国CERA、华中电信等机房资源,月神科技有自己的用户群和拥有创宇认证,并且也有电商企业将业务架设在月神科技的平台上。目前,香港CN2云服务器、洛杉矶CN2云主机、华中电信高防vps,月付20元起。点击进入:月神科技官方网站地址月神科技vps优惠信息:香港安畅CN2-GIA低至20元核心:2...

vdsina:俄罗斯VPS(datapro),6卢布/天,1G内存/1核(AMD EPYC 7742)/5gNVMe/10T流量

今天获得消息,vdsina上了AMD EPYC系列的VDS,性价比比较高,站长弄了一个,盲猜CPU是AMD EPYC 7B12(经过咨询,详细CPU型号是“EPYC 7742”)。vdsina,俄罗斯公司,2014年开始运作至今,在售卖多类型VPS和独立服务器,可供选择的有俄罗斯莫斯科datapro和荷兰Serverius数据中心。付款比较麻烦:信用卡、webmoney、比特币,不支持PayPal...

java读写文件为你推荐
ipad怎么分屏苹果ipad 分屏功能怎么使用0x80070005win7系统恢复时出现0x80070005错误代码怎么办啊1u在电子中 1u代表什么 为什么和931.5MeV对应 1u=931.4940MeV/c 这个公式的c又是什么 凌乱onboardon board是什么意思?wmiprvsewmiprvseoncontextmenuoncontextmenu="return false"是什么意思电子日历怎样调整电子日历上的时间bindserviceservice在手机程序中起什么作用smartuploadSmartUpload组建实现文件上传下载,我要把文件保存到项目中的某个文件夹中,该如何实现?最好有程序参考欢迎页面欢迎屏幕!!!!
域名批量查询 warez duniu justhost 博客主机 BWH 紫田 名片模板psd 12u机柜尺寸 小米数据库 lol台服官网 阿里校园 免费网页空间 上海联通宽带测速 常州联通宽带 超级服务器 宏讯 外贸空间 太原联通测速 路由跟踪 更多