0

我正在尝试让程序正常工作。输入是带有文本行的源文件。输出是一个带有原始文本行的目标文件,但相反。

ex. 
abcd  -->  dcba
efgh       hgfe

1234       4321

我看过几个类似的问题,但他们以与我不同的方式解决这个问题,这并不能完全解决这个个人问题。我已经通读了,我想我只是在想这个。我将非常感谢有关为什么我的代码根本没有输出到目标文件的输入。我做了一个堆栈跟踪,它一直打印得非常好。

谢谢,

代码:(命令行参数:source2.txt target2.txt

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java. util.Scanner;

/**
   This program copies one file to another.
*/
public class Reverse
{
   public static void main(String[] args) throws IOException
   {
      try{
      String source = args[0];
      String target = args[1];

      File sourceFile=new File(source);

      Scanner content=new Scanner(sourceFile);
      PrintWriter pwriter =new PrintWriter(target);

      while(content.hasNextLine())
      {
         String s=content.nextLine();
         StringBuffer buffer = new StringBuffer(s);
         buffer=buffer.reverse();
         String rs=buffer.toString();
         pwriter.println(rs);
      }
      content.close();    
      pwriter.close();
      }

      catch(Exception e){
          System.out.println("Something went wrong");
      }
   }
}
4

3 回答 3

4

你看到什么输出?

PrintWriter 抑制IOException并设置错误标志;您应该使用 OutputStreamWriter()。

此类中的方法从不抛出 I/O 异常,尽管它的某些构造函数可能会。客户端可以通过调用 checkError() 来查询是否发生了任何错误。

另外,不要用“出现问题”来处理异常;至少转储堆栈跟踪,以便您知道哪里出了问题。

也就是说,我可能会将读取的每一行输出到控制台,如下所示:

System.out.println("** Read ["+s+"]");

确认我实际上正在阅读该文件。

于 2012-12-08T02:17:41.883 回答
0

我对你的代码做了一些修改..

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;


public class Reverse
{
public static void main(String[] args) throws IOException
{
  try{
 // String source = args[0];
 // String target = args[1];

  File sourceFile=new File("C:/Users/Ruchira/Downloads/in.txt");//input File Path
  File outFile=new File("C:/Users/Ruchira/Downloads/out.txt");//out put file path

  Scanner content=new Scanner(sourceFile);
  PrintWriter pwriter =new PrintWriter(outFile);

  while(content.hasNextLine())
  {
     String s=content.nextLine();
     StringBuffer buffer = new StringBuffer(s);
     buffer=buffer.reverse();
     String rs=buffer.toString();
     pwriter.println(rs);
  }
  content.close();    
  pwriter.close();
  }

  catch(Exception e){
      System.out.println("Something went wrong");
  }
}
}

这将起作用

于 2012-12-08T06:58:15.730 回答
0
import java.io.*;
import java.util.*;

class Driver {

public static void main(String[] args) {
    ReverseFile demo = new ReverseFile();
    demo.readFile("source2.txt");
    demo.reverse("target2.txt");
}
}

class ReverseFile {

// Declare a stream of input
DataInputStream inStream;

// Store the bytes of input file in a String
ArrayList<Character> fileArray = new ArrayList<Character>();

// Store file sizes to see how much compression we get
long inFileSize;
long outFileSize;

// Track how many bytes we've read. Useful for large files.
int byteCount;

public void readFile(String fileName) {
             try {
        // Create a new File object, get size
        File inputFile = new File(fileName);
        inFileSize = inputFile.length();

        // The constructor of DataInputStream requires an InputStream
        inStream = new DataInputStream(new FileInputStream(inputFile));
    }

    // Oops.  Errors.
    catch (FileNotFoundException e) {
        e.printStackTrace();
        System.exit(0);
    }


    // Read the input file
    try {

        // While there are more bytes available to read...
        while (inStream.available() > 0) {

            // Read in a single byte and store it in a character
            char c = (char)inStream.readByte();

            if ((++byteCount)% 1024 == 0)
                System.out.println("Read " + byteCount/1024 + " of " + inFileSize/1024 + " KB...");

            // Print the characters to see them for debugging purposes
            //System.out.print(c);

            // Add the character to an ArrayList
            fileArray.add(c);
        }

        // clean up
        inStream.close();
        System.out.println("Done!!!\n");
    }

    // Oops.  Errors.
    catch (IOException e) {
        e.printStackTrace();
        System.exit(0);
    }

    // Print the ArrayList contents for debugging purposes
    //System.out.println(fileArray);
}


public void reverse(String fileName) throws IOException {
        FileWriter output = new FileWriter(fileName);

        for (int i = fileArray.size() - 1; i >= 0; i++) {
            try {
                output.write(fileArray.get(i));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        output.close();
    }
}

那应该行得通。如果没有,请告诉我,我会进一步调查问题。

于 2012-12-08T02:35:35.207 回答