当前位置: 技术问答>java相关
文件流问题
来源: 互联网 发布时间:2015-05-02
本文导语: 查找字符串,找到正常,没找到死机import java.io.*; public class e2 { public static void main(String[] args) { String temp=""; String destin; char c; int P=1,S=1,W=1; ...
查找字符串,找到正常,没找到死机import java.io.*;
public class e2
{
public static void main(String[] args)
{
String temp="";
String destin;
char c;
int P=1,S=1,W=1;
boolean found=false;
try{
FileInputStream text=new FileInputStream("text.txt");
DataInputStream in=new DataInputStream(new BufferedInputStream(System.in));
System.out.println("input word to be searched for:");
destin=in.readLine();
while((c=(char)text.read())!=-1)//此处有错?
{
switch(c)
{
case ' ':
case ',': temp="";W++;continue;
case '.':
case ';':
case '?':
case '!': temp="";W=1;S++;continue;
case 't':
case 'n': temp="";W=1;S=1;P++;continue;
default: temp+=c;
}
if(temp.equals(destin))
{
found=true;
break;
}
}
text.close();
}
catch(FileNotFoundException e)
{
System.err.println(e);
return;
}
catch(IOException e)
{
System.err.println(e);
return;
}
if(found)
System.out.println(destin+" found at:"+"nParagraph:"+P+"nSentence:"+S+"nWord:"+W+"n-------------n");
else
System.out.println(destin+"not found"+"n-------------n");
}
}
public class e2
{
public static void main(String[] args)
{
String temp="";
String destin;
char c;
int P=1,S=1,W=1;
boolean found=false;
try{
FileInputStream text=new FileInputStream("text.txt");
DataInputStream in=new DataInputStream(new BufferedInputStream(System.in));
System.out.println("input word to be searched for:");
destin=in.readLine();
while((c=(char)text.read())!=-1)//此处有错?
{
switch(c)
{
case ' ':
case ',': temp="";W++;continue;
case '.':
case ';':
case '?':
case '!': temp="";W=1;S++;continue;
case 't':
case 'n': temp="";W=1;S=1;P++;continue;
default: temp+=c;
}
if(temp.equals(destin))
{
found=true;
break;
}
}
text.close();
}
catch(FileNotFoundException e)
{
System.err.println(e);
return;
}
catch(IOException e)
{
System.err.println(e);
return;
}
if(found)
System.out.println(destin+" found at:"+"nParagraph:"+P+"nSentence:"+S+"nWord:"+W+"n-------------n");
else
System.out.println(destin+"not found"+"n-------------n");
}
}
|
while((c=(char)text.read())!=(char)-1)