[Java] Enter arrays from files, sorted and exported file in Java – Import Array from file, sort and export file in Java
– Create 1 class array initialization (class InitArray)
– Create 1 Import Export array class inherits from the class file to initialize the array (class IOArray extends InitArray)
– Create 1 sorted array class inheriting class array initialization (class SortArray extends InitArray)
– Class arrays (Array) contains the main function() program execution
Here is the code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
class InitArray
{
int [] Element;
}
class IOArray extends InitArray
{
void input_file() throws FileNotFoundException
{
FileInputStream fi = new FileInputStream("input.txt");
Scanner inp = new Scanner(fi,"UTF-8");
String temp = inp.nextLine(); //doc dong mang trong file
inp.close();
String [] item = temp.split(" "); //tach chuoi thanh cac phan tu chuoi
Element = new int[item.length];
for(int i=0; i<item.length; i++) //doi kiem string sang int cua cac phan tu
Element[i] = Integer.parseInt(item[i]);
}
void output_file() throws IOException
{
FileOutputStream fo = new FileOutputStream("output.txt");
PrintWriter out = new PrintWriter(fo);
for (int i=0; i<Element.length; i++)
out.printf("%-5d",Element[i]);
out.close();
}
}
class SortArray extends InitArray
{
public void sort(IOArray A) //phuong thuc sap xep
{
Arrays.sort(A.Element);
}
}
class Array
{
public static void main(String[] args) throws IOException
{
IOArray Arr = new IOArray();
Arr.input_file(); //nhap tu file
SortArray SArr = new SortArray();
SArr.sort(Arr); //sap xep
Arr.output_file(); //xuat ra file
System.out.println("nSucces ! Open file output.txt to view");
}
}



people ask me how to extend class class SortArray InitArray?
Ah so I can really only use arrays, there is also no need to inherit.
and I liked the fact that :in
alo cho mk xin face mk hoi chut dk k
What you chat with me via: fb.com/nguyenvanquan7826