Java I/O application design

Source: Internet
Author: User

Course Java object-oriented programming experiment name Java I/O application Design page

Class Ji Samban

First, the purpose of the experiment

Mastering the use of data streams

Second, the experimental environment

1, microcomputer one set

2.WINDOWS operating system , Java sdk,eclipse development environment

Iii. contents of the experiment

1, using the scanner class, the implementation of input 10 integers from the keyboard, and then sorted the integer, stored in the file, and then read from the file, displayed on the screen.

2, the contents of the file F1.txt encrypted, stored in F2.txt, read F2.txt, and display the decrypted content.

Iv. Experimental Steps and Results

1, using the scanner class, the implementation of input 10 integers from the keyboard, and then sorted the integer, stored in the file, and then read from the file, displayed on the screen.

(1) Program design ideas are as follows:

① uses the scanner class to enter 10 integers from the keyboard via a command prompt into an array of arr.

' Use the sort () method of the Java.util.Arrays class to sort the array in ascending order.

ƒ using DataOutputStream to create a connection to the specified file

(d:\\iotest\\shenxiaolinioapp.dat) The data output stream object, and the sorted array is stored in the file.

④ uses Dataintputstream to read the contents of the file and display the contents on the screen.

(2) The specific code of the program design is as follows:

Package integersort;

Import Java.io.ByteArrayInputStream;

Import Java.io.DataInputStream;

Import Java.io.DataOutputStream;

Import Java.io.FileInputStream;

Import java.io.FileNotFoundException;

Import Java.io.FileOutputStream;

Import java.io.IOException;

Import Java.io.InputStream;

Import Java.util.Scanner;

Import Java.util.Arrays;

public class Integersorttest {

/** * @param args*/

public static void Main (string[] args) {

SYSTEM.OUT.PRINTLN ("-------------use the Scanner class, enter 10 integers from the keyboard, sort, save the file, and then read the display on the screen-------------");

Scanner s=new Scanner (system.in);

System.out.println (">>> Please enter 10 integers <<<");

int[] Arr=new int [10];

for (int i = 0; i<arr.length; i++) {

System.out.print ("Please enter" + (i+1) + "integer:");

Arr[i]=s.nextint ();

}

System.out.println (">>> the 10 integers you entered before sorting for:<<<");

for (int i = 0; i < arr.length; i++) {

System.out.print (arr[i]+ "");

}

Arrays.sort (arr);//Sort an array of arr in ascending order (the sort () method of the Arrays class is used to

Method of ascending array of arrays)

System.out.println ("\n>>> is:<<< after sorting");

for (int i = 0; i < arr.length; i++) {

System.out.print (arr[i]+ "");

}

DataOutputStream Dos=null;

DataInputStream Dis=null;

try {

Creates a data output stream object that connects to the specified file

Dos=new DataOutputStream (New FileOutputStream ("D:\\iotest\\shenxiaolinioapp.dat"));

for (int i = 0; i < arr.length; i++) {

Dos.writeint (Arr[i]);

}

System.out.println ("\n\n>>> above 10 sorted integers have been successfully written to the file ' ShenxiaolinIOApp.dat '!") <<< ");

Read from a file to the screen

SYSTEM.OUT.PRINTLN ("\n>>> reads from the file as shown below:<<<");

Dis=new DataInputStream (New FileInputStream ("D:\\iotest\\shenxiaolinioapp.dat"));

for (int i = 0; i < arr.length; i++) {

Arr[i]=dis.readint ();

System.out.print (arr[i]+ "");

}

} catch (FileNotFoundException e) {

E.printstacktrace ();

}catch (IOException e) {

E.printstacktrace ();

}finally{

Close Stream Object

try {

if (Null!=dos) {

Dos.close ();//When the filter stream is closed, the underlying node stream that it wraps is automatically closed

}

} catch (Exception e) {

E.printstacktrace ();

}

try {

if (dis!=null) {

Dis.close ();

}

} catch (Exception e) {

E.printstacktrace ();

}

}

}

}

(3) The debug program, the results of the operation are as follows:

Among them, the contents of the file D:\\iotest\\shenxiaolinioapp.dat are as follows:

(Because DataInputStream and dataoutputstream use a byte-stream method, so write

Into and read files are binary files, using Notepad to open garbled is normal phenomenon)

2, the contents of the file F1.txt encrypted, stored in F2.txt, read F2.txt, and display the decrypted content.

(1) Program design ideas are as follows:

① creates the file in the D:\\iotest\\encryption\\f1.txt directory and writes the content in F1.txt.

' Write an encryption program to encrypt the contents of the F1.txt file and deposit it into the f2.txt in the same directory.

ƒ writes the program to read the F2.txt, simultaneously decrypts, and displays the decrypted content on the console.

(2) The specific code of the program design is as follows:

Package encryption;

Import java.util.*;

Import Java.io.BufferedInputStream;

Import Java.io.BufferedOutputStream;

Import Java.io.File;

Import Java.io.FileInputStream;

Import java.io.FileNotFoundException;

Import Java.io.FileOutputStream;

Import Java.security.PublicKey;

public class EncryptionTest1 {

public static void Main (string[] args) {

try {

Encrypt ();//encryption

Decrypt ();//decryption

} catch (Exception e) {

E.printstacktrace ();

}

}

After encrypting the contents of the file F1.txt, it is stored in f2.txt

private static void Encrypt () throws exception{

File F1=new file ("D:\\iotest\\encryption\\f1.txt");

File F2=new file ("F2.txt");

This word does not really create *.txt this file, but instead creates a representative *.txt

For this file, you need to determine if the file does not exist, and then create

if (!f1.exists ()) {

System.out.println ("F1.txt file does not exist!");

Return

}

if (!f2.exists ()) {

F2.createnewfile ();

}

FileInputStream fin=new FileInputStream (F1);

Bufferedinputstream bis=new Bufferedinputstream (Fin);

FileOutputStream fout=new FileOutputStream (New File ("D:\\iotest\\encryption\\f2.txt"));

Bufferedoutputstream bos=new Bufferedoutputstream (fout);

int b;

while ((B=bis.read ())!=-1) {

Bos.write (Encrypt.encrypt ((byte) b));

}

Bos.close ();

Bis.close ();

System.out.println (">>> file D:\\iotest\\encryption\\f1.txt content is encrypted and successfully deposited in D:\\iotest\\encryption\\f2.txt! ");

}

Reads the F2.txt and displays the decrypted content on the console.

private static void Decrypt () throws exception{

File File=new file ("F2.txt");

if (!file.exists ()) {

System.out.println ("F2.txt file does not exist!) ");

Return

}

FileInputStream fin=new FileInputStream ("D:\\iotest\\encryption\\f2.txt");

Bufferedinputstream bis=new Bufferedinputstream (Fin);

System.out.println (">>> decrypted content after reading the encrypted file F2.txt is as follows:");

Byte[] Buffer=new byte[2];

while (bis.read (buffer) >0) {

Buffer[0]=encrypt.decrypt (Buffer[0]);

Buffer[1]=encrypt.decrypt (buffer[1]);

System.out.print (new String (buffer));

}

Bis.close ();

}

}

Class encrypt{//This is an encryption class that contains the encryption (Encrypt), decryption (Decrypt) methods.

public static byte Encrypt (byte b) {

if ((b>=65 && b<=88) | | (b>=97 && b<=120)) {

Return (Byte) (b+2);

}else if (b>=89 && b<=90) | | (b>=121 && b<=122)) {

Return (Byte) (b-24);

}else {

return b;

}

}

public static byte Decrypt (byte b) {

if ((b>=67 && b<=90) | | (b>=99 && b<=122)) {

Return (Byte) (b-2);

}else if (b>=65 && b<=66) | | (b>=97 && b<=98)) {

Return (Byte) (b+24);

}else {

return b;

}

}

}

Where the file F1.txt content is:

(3) Debug the program, run the code, the results are as follows:

F1.txt The encrypted content is written f2.txt :

V. Summary of the Experiment

1. The experiment was completed on time and in quantity.

2. Through this experiment, I have deepened my knowledge of file and data flow, and have a further understanding of the file read and write operation.

Java I/O application design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.