Question

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 class, when instantiated, will write a string to a file by using a PrintStream object connected to a FileOutputStream Object. This cd goes in main WriteString ws = new WriteString(“f1.txt”,“Hello world”); All other code goes in the constructor. 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. This code goes in main int myArr[] = {16, 31, 90, 45, 89}; WriteInts wi = new WriteInts(“mydata.dat”, myArr); All other code goes in the constructor. 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. This code goes in main ReadInts ri = new ReadInts(“mydata.dat”); All other code goes in the constructor.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// import required package file
import java.io.File;

// The Filedetails class
public class FileDetails {
  
   // Constructor that performs the action
   public FileDetails(String filename)
   {
       // File object to get details
       File file=new File(filename);
       // printing obsolute path
       System.out.println("Absolute path of file:"+file.getAbsolutePath());
       // printing file size in bytes
       System.out.println("File Size in bytes:"+file.length());
       // Checking if file is readable
       if(file.canRead())
       System.out.println("The file is readable");
       else
          
           System.out.println("The file is not readable");
       // checking if file is writable
       if(file.canWrite())
           System.out.println("The file is writable");
       else
           System.out.println("The file is not writable");
   }
  
// main method goes here
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       // object instantiation
       FileDetails fd=new FileDetails("anyfile.doc"); // You should give exact path and file name here
  

   }

}

// Importing required packages
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

// the writestring class
public class WriteString {
  
   // Writestring class
   public WriteString(String filename,String text) throws FileNotFoundException
   {
       // File object
       File file=new File(filename);
       // Printsteam class that connects file using file output stream
       PrintStream ps=new PrintStream(new FileOutputStream(file));
       // using println method to write to the file
       ps.println(text);
      
   }
   // main method to execute the code
   public static void main(String[] args) throws FileNotFoundException
   {
       // the writestring class instantiation
       WriteString ws=new WriteString("f1.txt","Hello World");  // You should give exact path and file name here
   }

}

// importing packages
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

// The writints class
public class WriteInts {
  
   // the constructor to instantiate
   public WriteInts(String filename,int[] myArr) throws FileNotFoundException
   {
       // File object
       File file=new File(filename);
       // Using print stream to write integers
       PrintStream ps=new PrintStream(new FileOutputStream(file));
       //Looping through array elements to write every element
       for(int i=0;i<myArr.length;i++)
           ps.print(myArr[i]+" ");
   }
   // main goes here

   public static void main(String[] args) throws FileNotFoundException {
       // TODO Auto-generated method stub
       // Required lines in the main
       int myArr[] = {16, 31, 90, 45, 89};
       WriteInts wi = new WriteInts("mydata.dat", myArr);   // You should give exact path and file name here

   }

}

// import required packages
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

// The readints class
public class ReadInts {
  
   // the constructor
   public ReadInts(String filename) throws IOException
   {
       // file object
       File file=new File(filename);

       // This time we are using buffered reader to read from the file
       BufferedReader br=new BufferedReader(new FileReader(file));
       // line to store the entire line from the file
       String line;
       // String array to store ints in the form of strings
       String[] sarr=null;
       // Buffered reader to read lines
       while((line=br.readLine())!=null)
       {
           // Splitting line using space
           // Remeber we have used a space to seperate number
           // while writing to file in the othe class
          
           sarr=line.split(" ");
       }
       // Creating a new array
       int[] myArr=new int[sarr.length];
       // Using for loop to store ints in form string to integers
       for(int i=0;i<sarr.length;i++)
       {
           // Using parseint method to convert string to int
           myArr[i]=Integer.parseInt(sarr[i]);
       }
      
   }

   public static void main(String[] args) throws IOException {
       // TODO Auto-generated method stub
       ReadInts ri = new ReadInts("mydata.dat");  // You should give exact path and file name here

   }

}

Add a comment
Know the answer?
Add Answer to:
using java File Details – Build a class called FileDetails.java. When you instantiate this class and...
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 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...

  • 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”);     ]

  • The Java class called Holiday is started below. An object of class Holiday represents a holiday...

    The Java class called Holiday is started below. An object of class Holiday represents a holiday during the year. This class has three instance variables: name, which is a String representing the name of the holiday day, which is an int representing the day of the month of the holiday month, which is a String representing the month the holiday is in public class Holiday { private String name; private int day; private String month; // your code goes here...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • 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...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

  • 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...

  • You are to write a Java program using the following instructions to build a bank-ATM server...

    You are to write a Java program using the following instructions to build a bank-ATM server system with sockets. A bank holds accounts that can have deposits, withdrawals, or retrievals of balance. The bank server provides the account for transactions when a client uses and ATM machine. The ATM asks for the type of transaction and the amount (if needed), then creates a socket connection to the bank to send the transaction for processing. The bank performs the transaction on...

  • Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic...

    java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...

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