Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何读取文件、反转顺序和反转顺序写入_Java_File Io_Indexoutofboundsexception - Fatal编程技术网

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

Java 如何读取文件、反转顺序和反转顺序写入,java,file-io,indexoutofboundsexception,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(S

就像我做的一个类似的项目一样,这个项目从一个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");
          }
       }
}
下面是stacktrace的信息:

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

运行程序时,需要在命令行上指定文件名(源和目标)

java ReverseFile source.txt target.txt
在程序中,您尝试从命令行读取文件名,如下所示:

String source = args[0];
String target = args[1];

因此,如果您没有在那里指定这些名称,
java
尝试访问索引0和1处的数组
args
,它们是空的,您会得到
ArrayIndexOutOfBoundsException

我不太确定您的环境,以及文本的长度。我也不太清楚你为什么需要扫描仪

不管怎样,这是我对这个问题的看法,希望这对你有所帮助:)


这是您解决问题的无错误解决方案,您使用的是“Scanner”,而没有导入“util” 套餐。我们开始吧:-----------


只是想到了一个简单的方法

public class ReadFileReverse {

public int[] readByte(File _file) throws IOException {
    FileInputStream source = new FileInputStream(_file);
    int currentByte = source.available();
    int readCount = 0;

    int byteContainer[] = new int[currentByte];
    while(readCount < currentByte){
        byteContainer[readCount] = source.read();
        readCount++;
    }
    source.close();
    return byteContainer;
}

public void printReverse(int[] fileContent){
    for(int byt=fileContent.length -1; byt >= 0 ; byt--){
        System.out.print((char) fileContent[byt]);
    }
}

public static void main(String[] args) throws IOException {
    File fileToRead = new File("/README.txt");

    ReadFileReverse demo = new ReadFileReverse ();
    int[] readBytes = demo.readByte(fileToRead);

    demo.printReverse(readBytes);
}

}
公共类ReadFileReverse{
public int[]readByte(文件_文件)引发IOException{
FileInputStream源=新的FileInputStream(_文件);
int currentByte=source.available();
int readCount=0;
int byteContainer[]=新的int[currentByte];
while(读计数<当前字节){
字节容器[readCount]=source.read();
readCount++;
}
source.close();
返回字节容器;
}
public void printReverse(int[]fileContent){
for(int byt=fileContent.length-1;byt>=0;byt--){
System.out.print((char)fileContent[byt]);
}
}
公共静态void main(字符串[]args)引发IOException{
File fileToRead=新文件(“/README.txt”);
ReadFileReverse demo=newreadfilereverse();
int[]readBytes=demo.readByte(fileToRead);
printReverse(readBytes);
}
}

在这里,我们在字符串变量中读取一个文件,然后创建一个字符串生成器对象来高效地执行反向操作,然后打印

package-com;
导入java.io.FileReader;
公共班机{
公共静态void main(字符串[]args){
试一试{
FileReader fr=新文件读取器(“D:\\newfile.txt”);
字符串str=“”;
int-ch;
//读取字符串变量中的字符
而((ch=fr.read())!=-1){
str+=Character.toString((char)ch);
}
System.out.println(“原始字符串:“+str”);
//将字符串变量转换为字符串生成器对象
StringBuilder sb=新的StringBuilder(str);
//反转字符串并打印
System.out.println(“反向顺序:+sb.Reverse());
fr.close();
}捕获(例外e){
System.out.println(“错误”);
}
}

}
堆栈跟踪将有助于..1)将
catch(异常e){System.out.println(“出错”);
更改为
catch(异常e){e.printStackTrace();System.out.println(“出错”);
并将输出复制/粘贴为。2)对代码块使用一致的逻辑缩进。代码缩进旨在帮助人们理解程序流程。您几乎不应该执行
捕获(例外e)
。这会丢失关键信息,当出现问题时,您需要这些信息来解决问题,例如首先导致问题的行号。被否决的投票者是否有点耐心进行编辑或解释他们为什么被否决?我很困惑。@ElizabethTurner-您在运行pr时是否传递了这两个文件名gram?这是我按照建议运行时得到的异常:
线程“main”java.io.FileNotFoundException:\file.txt(系统找不到指定的文件)java.io.FileInputStream.open(本机方法)java.io.FileInputStream.(FileInputStream.java:138)ReverseFile.main(ReverseFile.java:49)
这些文件与类文件在同一目录下吗?是的,它们都与类文件在同一目录下。如果有,文件名是in.txt和out.txtdifference@ElizabethTurner-然后您需要在命令行上使用这些名称。您仍然会收到相同的异常吗?因为您在上面发布的异常注释,似乎java正在尝试读取“file.txt”,实际上我刚刚尝试了你的代码,我得到了一个类似于我的错误。可能是我吗?
java.lang.ArrayIndexOutOfBoundsException:0 at ReverseFile.main(ReverseFile.java:50)
我将发布一个修改后的版本,以打印更多有用的内容…等几分钟实际上我键入的cmdline错误,您的代码可以工作,但输出都在一行上。我说的离政府工作很近。您好。您可以将其更改为逐行执行。此代码将LINBREAK与普通字符一样处理。如果您在windows上,这s可能会很奇怪,因为在windows上,换行符实际上是两个字符(\r\n),如果将其反转,则会出现一个\n\r…我不太确定在这种情况下会发生什么。
   import java.io.*;
    import java.util.*;

        public class ReverseFile
         {
         public static void main(String[] args) throws IOException
          {
      try{
      File sourceFile=new File(args[0]);
      Scanner content=new Scanner(sourceFile);
      PrintWriter pwriter =new PrintWriter(args[1]);

      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");
      }
   }
      }
public class ReadFileReverse {

public int[] readByte(File _file) throws IOException {
    FileInputStream source = new FileInputStream(_file);
    int currentByte = source.available();
    int readCount = 0;

    int byteContainer[] = new int[currentByte];
    while(readCount < currentByte){
        byteContainer[readCount] = source.read();
        readCount++;
    }
    source.close();
    return byteContainer;
}

public void printReverse(int[] fileContent){
    for(int byt=fileContent.length -1; byt >= 0 ; byt--){
        System.out.print((char) fileContent[byt]);
    }
}

public static void main(String[] args) throws IOException {
    File fileToRead = new File("/README.txt");

    ReadFileReverse demo = new ReadFileReverse ();
    int[] readBytes = demo.readByte(fileToRead);

    demo.printReverse(readBytes);
}

}