如何用java提取txt中所有冒号后面的内容 并且把提取出来的内容赋值给变量与实例进行对比

(txt文件很大 内容比较多,这里只是举例)把冒号后面的内容提取出来 并且赋值给变量 与本身存在的实例进行字符对比 如果 完全相同 则输出true并输出在另外一个文件中 万分感谢啦 !!!!

    提取txt冒号后面内容可以使用string的split方法,截取后面即可,关于内容与变量进行比较可以使用equal方法,示例如下:

public calss ceshi{
   public static void main(String[] args){
      Scanner input = new Scanner(new File("filepath"));//filepath写你的txt路径
      PrintWriter output = new PrintWriter(new File("output.txt"));
      while(input.hasNextLine())
      {
         String[] temp = input.nextLine().split(";");//这里必须保证每行都存在冒号,否则需要加一个判断条件
         output.write(temp[1]);
       }
      output.close;
      ceshi ce = new ceshi():
      temp.equal(ce);//进行比较
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-05-07
用reader 的readerline方法读取再用string的lastindex方法获取:的位置用substring截取其他的简单了用if判断等
第2个回答  2014-05-07
public class ReadTxt {
public static void main(String[] args) {
/*writer("hello:6666");
writer("hi:8888");*/
String str = "8888";//模拟自身存在的字符
read(new File("hello.txt"),str);
}
//写文件
public static void writer(File file,String str){
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream os= null;
BufferedOutputStream bos = null;
PrintWriter out = null;
try {
os = new FileOutputStream(file,true);
bos = new BufferedOutputStream(os);
out = new PrintWriter(bos,true);
out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally{
out.close();
}
}
//读文件
public static void read(File file,String str){
List<String> list = new ArrayList<String>();//用于存放取出来数据(冒号后面的数字)
if(!file.exists()){
return;
}
InputStream is = null;
Reader reader = null;
BufferedReader br = null;
try {
is = new FileInputStream(file);
reader = new InputStreamReader(is);
br = new BufferedReader(reader);
String s = null;
while((s=br.readLine())!=null){
s = s.substring(s.indexOf(":")+1);
list.add(s);
if(s.equals(str)){
System.out.println(true);
writer(new File("hi.txt"), str);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试是ok的本回答被提问者采纳
第3个回答  2014-05-07
File file=new File("读文件路径+文件名和后缀");
File file2=new FIle("写路径+文件名和后缀");
BufferedReader br=null;
BufferedWriter bw=null;
String mes=null;
try{
file2.createNewFIle();
br=new BufferedReader(new FileReader(file));
bw=new BufferedWriter(new FileWriter(file2));
while((mes=br.readline())!=null)
{
mes=mes.split(":")[1];
............判断的代码 你所说的和本身的实例是否相等
if(相等){
System.out.println("true”);
bw.write(mes,0,mes.length());
bw.flush();
}
}
}
catch(Exception e)
{
}本回答被网友采纳
第4个回答  2014-05-07
用reader 的readerline方法读取再用string的lastindex方法获取:的位置用substring截取其他的简单了用if判断等
第5个回答  2014-05-07
public class ReadTxt {
public static void main(String[] args) {
/*writer("hello:6666");
writer("hi:8888");*/
String str = "8888";//模拟自身存在的字符
read(new File("hello.txt"),str);
}
//写文件
public static void writer(File file,String str){
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStream os= null;
BufferedOutputStream bos = null;
PrintWriter out = null;
try {
os = new FileOutputStream(file,true);
bos = new BufferedOutputStream(os);
out = new PrintWriter(bos,true);
out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally{
out.close();
}
}
//读文件
public static void read(File file,String str){
List<String> list = new ArrayList<String>();//用于存放取出来数据(冒号后面的数字)
if(!file.exists()){
return;
}
InputStream is = null;
Reader reader = null;
BufferedReader br = null;
try {
is = new FileInputStream(file);
reader = new InputStreamReader(is);
br = new BufferedReader(reader);
String s = null;
while((s=br.readLine())!=null){
s = s.substring(s.indexOf(":")+1);
list.add(s);
if(s.equals(str)){
System.out.println(true);
writer(new File("hi.txt"), str);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试是ok的本回答被提问者采纳
第6个回答  2014-05-07
File file=new File("读文件路径+文件名和后缀");
File file2=new FIle("写路径+文件名和后缀");
BufferedReader br=null;
BufferedWriter bw=null;
String mes=null;
try{
file2.createNewFIle();
br=new BufferedReader(new FileReader(file));
bw=new BufferedWriter(new FileWriter(file2));
while((mes=br.readline())!=null)
{
mes=mes.split(":")[1];
............判断的代码 你所说的和本身的实例是否相等
if(相等){
System.out.println("true”);
bw.write(mes,0,mes.length());
bw.flush();
}
}
}
catch(Exception e)
{
}本回答被网友采纳
相似回答
大家正在搜