Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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 使用ArrayList和反转文件_Java_Arraylist - Fatal编程技术网

Java 使用ArrayList和反转文件

Java 使用ArrayList和反转文件,java,arraylist,Java,Arraylist,我目前正忙于如何处理我的代码。我必须打开一个文件,然后在一个while循环中,我必须一次得到每一行,然后我必须反转它,然后打印它。 这就是我到目前为止所做的: public class ReverseFileContents { public static void main(String[] args) throws FileNotFoundException { Scanner myScanner = new Scanner(System.in);

我目前正忙于如何处理我的代码。我必须打开一个文件,然后在一个while循环中,我必须一次得到每一行,然后我必须反转它,然后打印它。 这就是我到目前为止所做的:

 public class ReverseFileContents
  { 
    public static void main(String[] args) throws FileNotFoundException
    {
      Scanner myScanner = new Scanner(System.in);
      Scanner diskScanner = new Scanner (new File("reverse"));
      while (inputFile.hasNextLine())
      {
      }
    }
  }

这就是我所拥有的一切,在这一点上我迷失了方向,只是不知道使用arraylist和BufferedReader应该朝哪个方向走。有人能帮我进一步吗?

我相信这样就行了

File file = new File(filename);
Scanner in = new Scanner(file);
 while (in.hasNextLine()){
System.out.println(reverse(in.nextLine()));
}


这听起来像是家庭作业,所以我不会给你你需要的算法

以下是获取文件并逐行查看的一些技巧:

File filename = new File("file.txt");
Scanner file = new Scanner(filename);

ArrayList<String> lines = new ArrayList<String>();
while(file.hasNextLine()){
    String line = file.nextLine();
    //You can manipulate the line in here and do whatever you want
    //This can also store the data into an ArrayList for later if you need
    myReverse(line);
    lines.add(line);
}

reverse
不是正确的文件名。所以你会有麻烦的。好的,谢谢,我会改变的。关于arraylist和逐行打印文件还有什么其他建议吗?有什么原因需要使用arraylist吗?你是打算以后用这些数据做些什么,还是只想把它向后打印?这是一个我有问题的作业。我的教授希望从现在开始使用arraylist。我们所做的就是打印线条,让它们以正确的方式显示。ArrayList在哪里?这看起来不错,但我不能使用。反过来看。对不起,我刚才没提到。你必须用什么??要反转的自定义代码。?是的,我必须使用自己的代码或将其放入代码中的某个位置。这是一项我遇到困难的任务。谢谢,我只需要一些指导。@user3416645没问题。如果这就是你想要的,一定要接受它作为答案。接受一些答案和就StackOVerflow提出问题一样重要。
File filename = new File("file.txt");
Scanner file = new Scanner(filename);

ArrayList<String> lines = new ArrayList<String>();
while(file.hasNextLine()){
    String line = file.nextLine();
    //You can manipulate the line in here and do whatever you want
    //This can also store the data into an ArrayList for later if you need
    myReverse(line);
    lines.add(line);
}
public void myReverse(){
    //convert the string into an array
    //move through the array backwards printing the values
}