Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Arrays_Sorting - Fatal编程技术网

Java 将数字写入带有数组的文件,然后按顺序打印

Java 将数字写入带有数组的文件,然后按顺序打印,java,arrays,sorting,Java,Arrays,Sorting,我正在使用数组读/写文件。这是一个大学Java 2作业,我觉得我应该知道这一点,但我画了一个空白 我需要做的是将文件中的数字按升序排序 首先,程序检查src文件中是否存在某个文件: File file = new File("src/chapter12/randomfile.dat"); //File path if(file.exists()) { //If the file exists, print out each element on the fi

我正在使用数组读/写文件。这是一个大学Java 2作业,我觉得我应该知道这一点,但我画了一个空白

我需要做的是将文件中的数字按升序排序

首先,程序检查src文件中是否存在某个文件:

        File file = new File("src/chapter12/randomfile.dat"); //File path

    if(file.exists())
    {   //If the file exists, print out each element on the file.
        Scanner input = new Scanner(file);
        while(input.hasNextLine())
        {               
            System.out.println(input.nextLine());
        }
            input.close();
    }
^^这一部分非常适合搜索文件,如果发现它,则打印出它包含的行

然后,如果找不到该文件,我们将创建它并在此处写入100个随机数,从0到100:

else
    {   //If the file isn't found, create it and write 100 random numbers to it.
        try(PrintWriter output = new PrintWriter(file))
        {
            for(int i = 0; i < size; i++)
            {
                randomArray[i] += random.nextInt(101);
                output.print(randomArray[i] + " ");
            }
        }
这里呢

Scanner input = new Scanner(file);
        Arrays.sort(randomArray);
        while(input.hasNextLine())
        {               
            System.out.println(input.nextLine());
        }
            input.close();
第一个位置在循环中,它将在每次迭代结束之前对其进行排序。 第二个是在我检查文件之前,因为程序第一次运行时,文件不存在,所以数组是空的。但是,由于文件是在第一次运行之后创建的,因此从逻辑上讲,它将在读取文件之前对已满的数组进行排序,但这没有意义,因为它将对数组进行排序,但由于文件是创建的,因此无法对文件进行排序

目前,我正在尝试在打印出读取内容之前搜索一种可能对文件进行排序的方法,但是我的作业说明“您可以使用array类对文件进行排序。”这是我画空白的地方,有什么建议吗

如果需要,下面是完整的代码片段:

package chapter12;

import java.io.*;           //Allows us to use File Writer.
import java.util.Arrays;    //Allows us to sort the array.
import java.util.Random;    //Allows us to get random numbers.
import java.util.Scanner;   //Allows us to read the file.
public class Excercise1215 
{
    public static void main(String[] args) throws FileNotFoundException 
    {
        int size = 100;
        int[] randomArray = new int[size];
        Random random = new Random();

    File file = new File("src/chapter12/randomfile.dat"); //File path

    if(file.exists())
    {   //If the file exists, print out each element on the file.
        Scanner input = new Scanner(file);
        Arrays.sort(randomArray);
        while(input.hasNextLine())
        {               
            System.out.println(input.nextLine());
        }
            input.close();
    }
    else
    {   //If the file isn't found, create it and write 100 random numbers to it.
        try(PrintWriter output = new PrintWriter(file))
        {
            for(int i = 0; i < size; i++)
            {
                randomArray[i] += random.nextInt(101);
                output.print(randomArray[i] + " ");
                Arrays.sort(randomArray);
            }
        }           
        //Exception handling
        catch(FileNotFoundException ex)
        {
            System.out.println("File was not found.");  
        }
        catch(Exception ex)
        {
            System.out.println("Something went wrong.");
        }
    }
}
package第12章;
导入java.io.*//允许我们使用文件编写器。
导入java.util.array//允许我们对数组进行排序。
导入java.util.Random//允许我们获取随机数。
导入java.util.Scanner//允许我们读取文件。
公开课练习1215
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
int size=100;
int[]随机数组=新的int[size];
随机=新随机();
File File=新文件(“src/chapter12/randomfile.dat”);//文件路径
if(file.exists())
{//如果文件存在,请打印文件上的每个元素。
扫描仪输入=新扫描仪(文件);
排序(随机数组);
while(input.hasNextLine())
{               
System.out.println(input.nextLine());
}
input.close();
}
其他的
{//如果找不到该文件,请创建该文件并向其写入100个随机数。
try(PrintWriter输出=新的PrintWriter(文件))
{
对于(int i=0;i

}

在写入文件之前,如果该文件不存在,则应在写入之前对数组进行排序。使用以下两个for循环执行此操作:

for(int i = 0; i < size; i++) {
    randomArray[i] += random.nextInt(101); //Populate the array
}
Arrays.sort(randomArray); // Sort the array
for(int i = 0; i < size; i++) {
    output.print(randomArray[i] + " "); // Write the array to the file
}
for(int i=0;i
如果文件确实存在,则应将值读入数组,对数组进行排序,然后打印数组。以下是一个示例:

if(file.exists())
{   //If the file exists, print out each element on the file.
    Scanner input = new Scanner(file);
    int count = 0;
    while(input.hasNextInt())
    {
        randomArray[count++] = input.nextInt();
    }
    input.close();
    Arrays.sort(randomArray);

    for(int i = 0; i < size; i++) {
        System.out.println(randomArray[i]);
    }
}
if(file.exists())
{//如果文件存在,请打印文件上的每个元素。
扫描仪输入=新扫描仪(文件);
整数计数=0;
while(input.hasNextInt())
{
randomArray[count++]=input.nextInt();
}
input.close();
排序(随机数组);
对于(int i=0;i
读取未排序的文件时,代码应该是这样的

 Scanner input = new Scanner(file);
 i = 0;
 while(input.hasNextInt())
{               
     // add to randomArray
     randomArray[i++] = input.nextInt();
}

Arrays.sort(randomArray);

// now iterate your sorted array and print out 
for (i = 0; i < randomArray.length; i++) {
   System.out.println(randomArray[i]);       
}
扫描仪输入=新扫描仪(文件);
i=0;
while(input.hasNextInt())
{               
//添加到随机数组
randomArray[i++]=input.nextInt();
}
排序(随机数组);
//现在迭代排序的数组并打印出来
对于(i=0;i
啊,谢谢!我的思维没有逻辑性,因为我在将数组键入文件后对数组进行排序。我想指出的是,程序会检查文件,如果找不到,则写入文件。我不需要再放两次,对吗?或者,如果我使用的是方法,我会这样做吗?我不确定我是否理解你的评论。你能澄清一下吗?
 Scanner input = new Scanner(file);
 i = 0;
 while(input.hasNextInt())
{               
     // add to randomArray
     randomArray[i++] = input.nextInt();
}

Arrays.sort(randomArray);

// now iterate your sorted array and print out 
for (i = 0; i < randomArray.length; i++) {
   System.out.println(randomArray[i]);       
}