社区
Java SE
帖子详情
一道 程序题!!
wang303044
2005-06-16 12:26:50
用java实现在一个文本文件中查找指定的字符串, 如命令 java Search "good" data.txt
则实现在data.txt中查找"good" 如果找到就返回所在行数和该行内容,
若不止一行则打印出所有这样的行。
...全文
126
4
打赏
收藏
一道 程序题!!
用java实现在一个文本文件中查找指定的字符串, 如命令 java Search "good" data.txt 则实现在data.txt中查找"good" 如果找到就返回所在行数和该行内容, 若不止一行则打印出所有这样的行。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
rower203
2005-06-16
打赏
举报
回复
加入行号:
public static void find(String content, String file){
BufferedReader readFile;
File f = new File(file);
if(!f.exists()){
System.out.println(file + " not exist!");
return;
}
try {
readFile = new BufferedReader(
new FileReader(file));
String line;
try {
int lineNum = 0;
line = readFile.readLine();
while(line != null) {
lineNum++;
if(line.indexOf(content) >= 0)
System.out.println("Line " + lineNum + ": " + line);
line = readFile.readLine();
}
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
// find("good", "data.txt");
find(args[0], args[1]);
}
rower203
2005-06-16
打赏
举报
回复
public static void main(String args[])
{
find(args[0], agrs[1]);
}
rower203
2005-06-16
打赏
举报
回复
public static void find(String content, String file){
BufferedReader readFile;
File f = new File(file);
if(!f.exists()){
System.out.println(file + " not exist!");
return;
}
try {
readFile = new BufferedReader(
new FileReader(file));
String line;
try {
line = readFile.readLine();
while(line != null) {
if(line.indexOf(content) >= 0)
System.out.println(line);
line = readFile.readLine();
}
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
find("good", "data.txt");
}
Z_Beginner
2005-06-16
打赏
举报
回复
import java.io.*;
public class test
{
public static void main(String args[])
{
char ch;
int chi;
int ox;
File MyFile1=new File("c:\\temp",args[1]+".txt");
String sub=args[0];
StringBuffer sb=new StringBuffer();
try
{
FileInputStream fin=new FileInputStream(MyFile1);
while((chi=fin.read())!=-1)
{
//System.out.print((char)chi);
sb.append((char)chi);
}
fin.close();
System.out.println(sb);
}
catch(FileNotFoundException e){System.err.println(e);}
catch(IOException e){System.err.println(e);}
String s=sb.toString();
for(int i=0;i<s.length();)
{
ox=s.indexOf(sub,i);
System.out.println(s.substring(ox,ox+3));
i=ox+3;
}
}
}
一道
算24点的
程序
设计
题
!!
本文介绍了一种计算24点的游戏算法,该算法利用四张扑克牌通过加、减、乘、除运算达到24的目标。文章详细阐述了游戏规则,并提出了解决方案的设计要求。
hpu 1695
一道
签到
题
本文深入解析了
一道
签到
题
的核心思路,教你如何通过KMP算法高效找出小数的循环节及其相关属性,包括循环次数、循环节长度等。详细步骤和代码实现,助你轻松应对类似问
题
。
第
一道
ACM
程序
题
本文介绍了一个简单的
程序
设计案例,通过输入一系列奶牛的产奶量,并使用C++实现,找出这些产奶量的中位数。该
程序
利用了动态数组和排序算法来高效解决问
题
。
分享
一道
百度笔试
程序
题
针对
一道
百度面试的算法
题
,该
程序
通过随机生成礼物喜好值并进行排序,确保保留K个最高价值的礼物。实现过程中考虑了礼物总数、保留数量及喜好值计算。
突然想到
一道
简单
题
,但也有深意!!
本文介绍三种求解1到n范围内能被3或5整除的数的和的方法:直接遍历求和法、优化遍历求和法及数学集合求和法,并对比了它们的效率。
Java SE
62,622
社区成员
307,257
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章