Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Java programming (urgently needed): Write a program, write two integers 100 and 200 into the file sample.dat with a data output stream, and then open the article.
Java programming (urgently needed): Write a program, write two integers 100 and 200 into the file sample.dat with a data output stream, and then open the article.
Packaging test 2;

Import java.io.bufferedreader;

Import java.io.bufferedwriter;

import Java . io . filenotfoundexception;

Import java.io.filereader;

Import java.io.filewriter;

Import java.io.ioexception;

Public class Test 1 {

//Write data to a file

Public static void writeToFile (string path)

Try {

buffered writer bw = new buffered writer(new file writer(path));

int a = 100;

int b = 200

//You need to convert plastic into string type.

bw . write(string . value of(a));

bw . flush();

bw . newline();

bw . flush();

bw . write(string . value of(b));

bw . flush();

bw . close();

} catch (IOException e) {

e . printstacktrace();

}

}

//Open the file and display the contents of the file to the console.

Public static void readFile (string path)

Try {

buffered reader br = new buffered reader(new file reader(path));

Try {

String line = null

while((line = br.readLine())! = null){

system . out . println(line);

}

} catch (IOException e) {

e . printstacktrace();

}

} catch(file not found exception e){

e . printstacktrace();

}

}

Public static void main(String[] args) {

write to file(" d:/sample . dat ");

readFile(" d:/sample . dat ");

}

}