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

どのような出力が表示されますか??

PrintWriterIOExceptionは、代わりにエラー フラグを抑制して設定します。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 に答える