[Java]Enter, made in Java – Input, output in Java
An easy entry and keyboard in Java with Scanner
import java.io.IOException; import java.util.*; //thu vien dung lop Scanner class java_in_out_put { public static void main(String agrv[]) throws IOException { String s; int age; Double M; Scanner inp = new Scanner(System.in); //tao doi tuong inp thuoc lop Scanner System.out.print("Insert your name : "); //Lenh in ra man hinh s = inp.nextLine(); //nhap chuoi System.out.print("Insert your age: "); age = inp.nextInt(); //nhap so nguyen System.out.println("Insert your Math: "); M = inp.nextDouble(); System.out.printf("My name is %s , I %d yaers old and I am %.2f math scoren", s, age, M); inp.close(); } }
Next, the output file. In this program I will read from the file consists of lines INPUT.TXT, each with the name, age, Salary separated by commas, then written to the file output.txt information on different lines:
CEO:
INPUT.TXT file content is
Nguyen Van Q,21,100000.0
Nguyen Thi B,20,200000.0
will be written to the file output.txt
Nguyen Van Q
21
100000.0
Nguyen Thi B
20
200000.0
import java.io.*; import java.util.*; class input_file { String ten[] = new String[100]; int tuoi[] = new int[100]; double luong[] = new double[100]; int i=0; void read() throws IOException //phuong thuc doc tu file { String line[] = new String[100]; FileInputStream f = new FileInputStream("input.txt"); //tao bien tep f Scanner input = new Scanner(f,"UTF-8"); //doc tu tep f su dung Scanner while(input.hasNextLine()) //trong khi chưa het file { line[i]= input.nextLine(); //doc 1 dong if(line[i].trim()!="") //neu dong khong phai rong { String item[] = line[i].split(","); //cat cac thong tin cua line bang dau phay ten[i] = item[0]; tuoi[i] = Integer.parseInt(item[1]); //chuyen strin sang int luong[i] = Double.parseDouble(item[2]); } i++; } input.close(); } void write() throws IOException //phuong thuc ghi vao file { FileOutputStream f = new FileOutputStream("output.txt"); PrintWriter output = new PrintWriter(f); int j=0; while(j<i) { output.println(ten[j]); output.println(tuoi[j]); output.println(luong[j]); j++; } output.close(); } } class java_in_out_file { public static void main(String []agr) throws IOException { input_file file = new input_file(); file.read(); file.write(); } }
a dear , cho e hỏi là viết hàm nhập va xuất 1 mảng số như thế nào ạ.e cảm ơn a nhiều:)))
Bạn dùng vòng lặp for để nhập nhé
a dear, cho e hỏi throws Exception với throws IOException khác nhau chỗ nào vậy a? With 2 cái đó dùng để làm gì? tks a nhiều 🙂
Exception exception of all exceptions chyng. IOException exception on Import Export.
My brother asked me
for example, your input file name, age, identity card number, I just want to pay the export output file name with the identity card alone is how ạ
Hope you reply soon
Thank you
So what part you only write files to write what you want.
Mình không hiểu, cùng là vào ra file nhưng Scanner/Prinwriter và FileInpuStream/FileOuputStream khác nhau như nào. Tại mình thấy dùng 2 cái đều ok.
Ủa, nó báo lỗi ở dòng 23, 24 “NumberFormatException” a Military dear
So certain that your files are somehow, it sure does not read the old news is not read and luơng. You see the input file nhé.
e revised okay a =)))
f fix then a =))
If you want to leave spaces in the input then how that he?
ie input is: Nguyen Van C, 20, 100000 that still print output code is
you split according to what you want is to be.
e want to enter an integer array from the e file will copy a
They do as usual, not only through reading from the file you.
e ask package in java mean sir ,if the user has been star k k sir
You can refer here while: https://nguyenvanhieu.vn/package-trong-java/
Star lengthy e see java vs trouble than c v ?? :((
Long but clear, gradually you will be familiar.