[英]Writing numbers to a file with an array then printing them sorted
我正在使用數組讀取/寫入文件。 這是針對大學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();
}
^^這部分非常適合搜索文件,然后如果發現文件,則打印出其中包含的行。
然后,如果找不到該文件,我們將創建它並在此處從0到100寫入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] + " ");
}
}
我嘗試在兩個地方輸入Array.sort(randomArray):
randomArray[i] += random.nextInt(101);
output.print(randomArray[i] + " ");
Arrays.sort(randomArray);
和這里
Scanner input = new Scanner(file);
Arrays.sort(randomArray);
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
input.close();
第一個位置在循環中,它將在每次迭代結束之前對其進行排序。 第二個是在我檢查文件之前,因為程序第一次運行時,該文件不存在,因此數組為空。 但是,由於文件是在第一次運行后創建的,因此從邏輯上講,它會在讀取文件之前對已滿的數組進行排序,但是這沒有意義,因為它將對數組進行排序,但是由於文件已創建,因此無法對文件進行排序。
目前,我正在嘗試搜索一種方法,以便在打印出讀取的內容之前對文件進行排序,但是我的作業指出“您可以使用數組類對文件進行排序”。 這是我的空白,有什么建議嗎?
如果需要,這是完整的代碼片段:
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.");
}
}
}
}
如果不存在,則在寫入文件之前,應先對數組進行排序。 使用兩個這樣的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
}
如果文件確實存在,則應將值讀入數組,對數組進行排序,然后打印數組。 這是一個示例:
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]);
}
}
讀取未排序的文件時,您的代碼應該是這樣的
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]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.