如何只从文本文件中获取包含两个字符的行 [英] How to get only a line containg two characters from text file

查看:144
本文介绍了如何只从文本文件中获取包含两个字符的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命令提示符下输出了输出。但是当我尝试在文本文件中获得答案时...我的编码构建成功...但显示空文件



我的尝试:



i got the output in command prompt.but when i try to get the answer in text file..my coding build successfully...but shows empty file

What I have tried:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class ll {

public static void main(String[] args) {
    FileReader fr = null;
    BufferedReader br =null;

    String [] stringArray;
    int counLine = 0;
    int arrayLength ;
    String s="";
    String stringLine="";
    try{
     //   fr = new FileReader("F:\\New folder (2)\\N.txt");
        String wrt="F:\\New folder (2)\\N.txt";
String line=null;
 fr=new FileReader(wrt);
FileWriter fw=new FileWriter(wrt);
BufferedWriter bw=new BufferedWriter(fw);
 
        br = new BufferedReader(fr);
        while((s = br.readLine()) != null){
            stringLine = stringLine + line;
            stringLine = stringLine + " ";
            counLine ++;  
        }System.out.println(stringLine);
 while((line=br.readLine())!=null)
       {
          System.out.print(line);
        }
        br.close();
        stringArray = stringLine.split(" ");
        arrayLength = stringArray.length;
     for (int i = 0; i < arrayLength; i++) {
            int c = 1 ;
            for (int j = i+1; j < arrayLength; j++) {
                if(stringArray[i].equalsIgnoreCase(stringArray[j])){
                   c++;
                   for (int j2 = j; j2 < arrayLength; j2++) {
                 }
     }
                int k;
              for(k=2;k==stringArray[i].length();i++)
           
             // if(stringArray[i].length()==1)
                {     bw.write(stringArray[i]);
              //    System.out.println(stringArray[i]);  
              }
            }}

//bw.newLine();
//bw.write("hello");
bw.close();
     fr.close();
        br.close();
    }catch (Exception e) {
        e.printStackTrace();
    }}}

推荐答案

您应该尽快学会使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较当。

当代码没有达到预期的效果时,你就接近一个bug。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


你的程序非常混乱,一些代码是多余的。您的第一个循环将文件的每一行读入名为 s 的变量中,但不对该数据执行任何操作。它反复尝试连接 stringLine ,但它们都不包含任何文本。然后你开始尝试读取文件的第二个循环,但由于它已经在EOF(从第一个循环),没有什么可读。之后,您尝试将某些内容写回同一文件,从而销毁任何原始数据。如果必须在同一程序中进行读写,则使用两个不同的文件,这样就不会损坏原始测试数据。另外,尝试逐行阅读您的程序以检查逻辑。
Your program is very confusing and some of the code is redundant. Your first loop reads each line of the file into the variable named s, but does nothing with that data. It repeatedly tries to concatenate stringLine and line, but neither of them contain any text. You then start a second loop trying to read the file, but since it is already at EOF (from the first loop) there is nothing to read. Following that you attempt to write something back to the same file, thus destroying any of the original data. If you must do reading and writing in the same program then use two different files so you do not corrupt your original test data. Also, try reading through your program line by line to check the logic.


这篇关于如何只从文本文件中获取包含两个字符的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆