Question

Using Java 1.) Write Integers to a File – This time build a class WriteInts. This...

Using Java

1.) Write Integers to a File – This time build a class WriteInts. This class, when instantiated, will create a new file and write an array of integers to this new file. All the code to write the data to the file goes in the Constructor.

[i.e.   // This code goes in main()

int myArr[] = {16, 31, 90, 45, 89};

        WriteInts wi = new WriteInts(“mydata.dat”, myArr);   ]

2.) Read Integers from a File – This time build a class ReadInts. This class, when instantiated, will read the integers from the file given, and print them to the Console. All the code to write the data to the file goes in the Constructor.

[i.e.   // This code goes in main()

        ReadInts ri = new ReadInts(“mydata.dat”);     ]

3.) Write a String to a File using PrintStream – This time build a class WriteString. This class, when instantiated, will write a string to a file by using a PrintStream object connected to a FileOutputStream Object.

[i.e. // This code goes in main()

WriteString ws = new WriteString(“f1.txt”,“Hello world”);]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class WriteInts {

    public WriteInts(String fileName, int arr[]){
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(fileName);
            for(int i = 0; i < arr.length; ++i) {
                printWriter.println(arr[i]);
            }
            printWriter.flush();
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
public class TestWriteInts {

    public static void main(String[] args) {
        int myArr[] = {16, 31, 90, 45, 89};
        new WriteInts("mydata.dat", myArr);
    }

}

as\;per\;guidelines\;we\;are\;only\;allowed\;to\;answer\;first\;question\\ Please\;make\;separate\;posts\;for\;remaining\;questions

Add a comment
Know the answer?
Add Answer to:
Using Java 1.) Write Integers to a File – This time build a class WriteInts. This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using java File Details – Build a class called FileDetails.java. When you instantiate this class and...

    using java File Details – Build a class called FileDetails.java. When you instantiate this class and give it a filename, it will report back the size of the file, whether the file is Readable and whether the file is Writeable; plus any other file information that you might deem important. This cd goes in main FileDetails fd=newFileDetails(“anyfile.doc”); All other code goes in the constructor. Write a String to a File using PrintStream – This time build a class WriteString. This...

  • Java practice

    Write a String to a File using PrintStream – This time build a class WriteString.  This class, when instantiated, will write a string to a file by using a PrintStream object connected to a FileOutputStream Object.[i.e.   // This code goes in main()WriteString ws = new WriteString(“f1.txt”,“Hello world”);]

  • Java practice

    Read Integers from a File – This time build a class ReadInts.  This class, when instantiated, will read the integers from the file given, and print them to the Console.  All the code to write the data to the file goes in the Constructor.[i.e.   // This code goes in main()        ReadInts ri = new ReadInts(“mydata.dat”);     ]

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters...

    Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters in it (see ExtraneousStringException.java. This java file does not need to be modified). Copy paste the contents of ExtraneousStringException.java into a new .java file, uncomment the commented out code, and write your code inside the main method. In the driver class, keep reading strings from the user until the user enters “DONE.” You may read your input from a file, or use the console....

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

  • 1. write a java program using trees(binary search treee) to read integers from input file( .txt)...

    1. write a java program using trees(binary search treee) to read integers from input file( .txt) and add +1 to each number that you read from the input file. then copy result to another (.txt) file. example: if inpu File has numbers like 4787 79434 4326576 65997 4354 the output file must have result of 4788 79435 4326577 65998 4355 Note: there is no commas in between the numbers. dont give images or theory please.

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT