Java PrintWriter不是';t将int值写入它创建的新文件中

Java PrintWriter不是';t将int值写入它创建的新文件中,java,Java,所以我应该读入一个.txt文件,然后写一个新的,数字倒过来。由于某些原因,新文件中只打印数字0,而该数字应为987654321987654321 编辑: 输入文件的内容如下: 1234567898123456789 import java.io.*; import java.util.*; class Reverb { public static void main(String[] args) throws IOException { //System.out.println("En

所以我应该读入一个.txt文件,然后写一个新的,数字倒过来。由于某些原因,新文件中只打印数字0,而该数字应为987654321987654321

编辑: 输入文件的内容如下: 1234567898123456789

import java.io.*;
import java.util.*;

class Reverb
{
public static void main(String[] args) throws IOException
{
    //System.out.println("Enter the name of the file you wish to open and reverse.");
    Scanner kb = new Scanner(System.in);
    //String s = kb.next();

    Scanner inFile = new Scanner(new File("Untitled.txt"));
    PrintWriter outFile = new PrintWriter("reversamundo2.txt");

    int[] a = new int[18];
    int index = 0;

    while(inFile.hasNextInt())
        {
            a[index] = inFile.nextInt();
            index++;
        }

    for(int i = index; i >= 0; i--)
    {
        outFile.println(a[i]);
    }

    inFile.close();
    outFile.close();
}
}

您希望逐字读取而不是字符串,然后将其反转。您只需读取字符串并使用StringBuilder将其反转即可。因此,代码如下所示:

Scanner inFile = new Scanner(new File("Untitled.txt"));
PrintWriter outFile = new PrintWriter("reversamundo2.txt");

// Read the first String from the input file.
String line = inFile.next();

// Create a StringBuilder object for that string, reverse it and return a string.
String reverse = new StringBuilder(line).reverse().toString();

// Print the reversed string to a new file.
outFile.print(reverse);

为什么它应该是
987654321987654321
?如果您需要作为
987654321987654321
全部放在一行中,那么您应该使用
outFile.print(a[i])而不是println。另外,请共享输入文件数据您认为hasnetint的作用是什么?您认为
int
中可以容纳的最大值是多少?