java - 如何读取文件、反转顺序和反转顺序写入

标签 java file-io indexoutofboundsexception

就像我做的一个类似的项目一样,这个项目是从一个txt文件中读取字符,反转字符串的顺序并将其重写到另一个txt文件中。但它不断输出我的异常“出了问题”。谁能帮我解决问题吗?

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

public class ReverseFile
{
    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();
          System.out.println("File is copied successful!");
          }

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

这是来自堆栈跟踪的信息:

java.lang.ArrayIndexOutOfBoundsException: 0
    at ReverseFile.main(ReverseFile.java:36)

最佳答案

我不太确定您的环境以及文本可能有多长。我也不太确定你为什么需要扫描仪?

无论如何,这是我对这个问题的看法,希望对你有帮助:)

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;


public class Reverse {

    public static void main(String[] args) {

        FileInputStream fis = null;
        RandomAccessFile raf = null;

        // by default, let's use utf-8
        String characterEncoding = "utf-8";

        // but if you pass an optional 3rd parameter, we use that
        if(args.length==3) {
            characterEncoding = args[2];
        }

        try{

            // input file
            File in = new File(args[0]);
            fis = new FileInputStream(in);

            // a reader, because it respects character encoding etc
            Reader r = new InputStreamReader(fis,characterEncoding);

            // an outputfile 
            File out = new File(args[1]);

            // and a random access file of the same size as the input, so we can write in reverse order 
            raf = new RandomAccessFile(out, "rw");
            raf.setLength(in.length());

            // a buffer for the chars we want to read 
            char[] buff = new char[1];

            // keep track of the current position (we're going backwards, so we start at the end)
            long position = in.length(); 

            // Reader.read will return -1 when it reached the end.
            while((r.read(buff))>-1) {

                // turn the character into bytes according to the character encoding
                Character c = buff[0];
                String s = c+"";
                byte[] bBuff = s.getBytes(characterEncoding);

                // go to the proper position in the random access file
                position = position-bBuff.length;
                raf.seek(position);

                // write one or more bytes for the character
                raf.write(bBuff);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // clean up
            try {
                fis.close();
            } catch (Exception e2) {
            }
            try {
                raf.close();
            } catch (Exception e2) {
            }
        }


    }


}

关于java - 如何读取文件、反转顺序和反转顺序写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16138380/

相关文章:

java.lang.IndexOutOfBoundsException : Index: 7, 大小 : 7. 为什么会发生这种情况?

java - Netty:MessageToMessageEncoder 并转换为 ByteBuf

java - 如果存在另一个匹配的数字(两个相同的数字),如何更改数组中的数字

java - 是否可以将不同的编译器嵌入到我的应用程序中?

c++ - 具有相对路径的 fopen()

java - 如何在 android 中解决 StringIndexOutOfBoundsException

java - CRLF 序列 ('CRLF Injection' 的不当中和)(CWE ID 93)

java - 如何创建一个将在 JVM 终止时自动删除的临时目录?

c# - 如何判断文件是否已*完全*写入

java JDBC ArrayIndexOutOfBoundsException 删除、更新时出现