In Java please, I need the text output file for this. import java.util.Random; i
ID: 3677161 • Letter: I
Question
In Java please,
I need the text output file for this.
import java.util.Random;
import java.io.*;
public class TestData {
public static void main (String [] args) throws IOException
{
final int MAX = 10;
int value;
String file = "test.dat";
Random rand = new Random();
FileWriter fw = new FileWriter (file);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);
for (int line=1; line <= MAX; line++)
{
for (int num=1; num <= MAX; num++)
{
value = rand.nextInt (90) + 10;
outFile.print (value + " ");
}
outFile.println ();
}
outFile.close();
System.out.println ("Output file has been created: " + file);
}
}
Explanation / Answer
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Textoutput { public static void main(String[] args) throws IOException { FileOutputStream fos = null; FileInputStream fis = null; byte[] b = {65,66,67,68,69}; int i=0; char c; try{ // create new file output stream fos=new FileOutputStream("C://test.txt"); // writes bytes to the output stream fos.write(b); // flushes the content to the underlying stream fos.flush(); // create new file input stream fis = new FileInputStream("C://test.txt"); // read till the end of the file while((i=fis.read())!=-1) { // convert integer to character c=(char)i; // prints System.out.print(c); } }catch(Exception ex){ // if an error occurs ex.printStackTrace(); }finally{ // closes and releases system resources from stream if(fos!=null) fos.close(); if(fis!=null) fis.close(); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.