Write a program called TextFileIO.java to create a file named numbers.dat. Then
ID: 3647681 • Letter: W
Question
Write a program called TextFileIO.java to create a file named numbers.dat. Then create an algorithm that writes all even numbered integers from 1 to 100, separated by a comma. After the file has been created, close and reopen the file and display the results to the screen. After the results have been displayed append the odd number integers from 1 to 100, separated by a comma to the end of the file. Reopen the file and display the results. The contents of the file should be the even numbers from 1 to 100 separated by a comma followed by the odd number from 1 to 100 separated by a comma. The output of this program would be something like the following2,4,6,8,10,12,14,
Explanation / Answer
please rate - thanks
import java.io.*;
import java.util.*;
class TextFileIO
{
public static void main(String args[])throws FileNotFoundException
{int i;
FileOutputStream output;
PrintStream out=new PrintStream(new File("numbers.dat"));
for(i=2;i<100;i+=2)
out.print(i+",");
out.print(i);
out.close();
Scanner input=new Scanner(new File("numbers.dat"));
System.out.println(input.nextLine());
input.close();
FileOutputStream out2; // declare a file output object
PrintStream p; // declare a print stream object
out2 = new FileOutputStream("numbers.dat",true);
p = new PrintStream( out2 );
for(i=1;i<100;i+=2)
p.print(","+i);
p.close();
Scanner in=new Scanner(new File("numbers.dat" ));
System.out.println(in.nextLine());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.