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

DMIT$10.9/月,日本VPS/三网直连/1核1.5G内存/20GB存储/1Gbps端口

优惠码年付一次性5折优惠码:TYO-Lite-Open-Beta-1y-50OFF永久8折优惠码:TYO-Lite-Open-Beta-Recur-20OFF日本vpsCPU内存SSD流量带宽价格购买1核1.5G20 GB4 TB1Gbps$10.9/月购买2核2 G40 GB6 TB1Gbps$16.9/月购买2核4 G60 GB8 TB1Gbps$21.9/月购买4核4 G80 GB12 TB...

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

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

IonSwitch:$1.75/月KVM-1GB/10G SSD/1TB/爱达荷州

IonSwitch是一家2016年成立的国外VPS主机商,部落上一次分享的信息还停留在2019年,主机商提供基于KVM架构的VPS产品,数据中心之前在美国西雅图,目前是美国爱达荷州科德阿伦(美国西北部,西接华盛顿州和俄勒冈州),为新建的自营数据中心。商家针对新数据中心运行及4号独立日提供了一个5折优惠码,优惠后最低1GB内存套餐每月仅1.75美元起。下面列出部分套餐配置信息。CPU:1core内存...

java读写文件为你推荐
空白代码html空格代码怎么写flowplayerswfobject.js这能不能播放音频啊(amr)smartuploadjspsmartupload如何使用?什么是SOA什么是中子刀?您了解吗?银联商务招聘急问银联商务的客服待遇如何啊?文件系统类型在windows系统中,常见的文件类型主要由哪几种?sungard李庆星老板咋样 我准备去CDMC上海决策者经济顾问公司上班了酷源码怎样看源码下优酷视频网站免费收录免费个人网站能给百度收录吗qq号免费申请免费申请QQ号
提供香港vps 域名备案号查询 新网域名管理 hkbn info域名 免费博客空间 论坛空间 国内php空间 adroit shopex主机 银盘服务 linode支付宝 我的世界服务器ip 东莞服务器托管 贵阳电信 金主 空间服务器 免费稳定空间 博客域名 SmartAXMT800 更多