Preview

Program in Java to read and write to a text file

Satisfactory Essays
Open Document
Open Document
289 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Program in Java to read and write to a text file
Reading from Standard Input
Use System.in
Use BufferedReader
Use read method
File Handling
Reading from a File
Use FileInputSt ream
Use read method
Writ ing to a File
Use FileOutputSt ream
Use write method

Sample program to input a textfile and display its contents import java.io.*; public class RFile { public static void main(String args[]) throws IOException { System.out.println("What is the name of the file to read from?"); String filename; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); filename = br.readLine(); System.out.println("Now reading from " + filename + "..."); FileInputStream fis = null; try { fis = new FileInputStream(filename); } catch (FileNotFoundException ex) { System.out.println("File not found."); } try { char data; int temp; do { temp = fis.read(); data = (char) temp; if (temp != -1) { System.out.print(data); } } while (temp != -1); System.out.println(); } catch (IOException ex) { System.out.println("Problem in reading from the file.");
}
}
}

Sample program to input a textfile and input any text and save it in that text file

import java.io.*; public class WFile { public static void main(String args[]) throws IOException { System.out.print("What is the name of the file to be written to? "); String filename; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); filename = br.readLine(); System.out.println("Enter data to write to " + filename + "..."); System.out.println("Type q$ to end."); FileOutputStream fos = null; try { fos = new FileOutputStream(filename); } catch (FileNotFoundException ex) { System.out.println("File cannot be opened for writing."); } try { boolean done = false; int data; do { data =

You May Also Find These Documents Helpful

  • Satisfactory Essays

    First, save this file to your hard drive by selecting Save As from the File menu. Click the white space below each question to maintain proper formatting…

    • 1331 Words
    • 6 Pages
    Satisfactory Essays
  • Powerful Essays

    1. The program will present a series of user screens that prompts the user for specified input.…

    • 1488 Words
    • 6 Pages
    Powerful Essays
  • Better Essays

    2. Create a Windows application that contains a button labeled OK and a textbox labeled Name. Change the…

    • 1713 Words
    • 7 Pages
    Better Essays
  • Satisfactory Essays

    |File – Is a place where data is stored on a computer, there are many different types of files and most if not all data is stored in a file. |…

    • 829 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    POS 420 Entire Course

    • 583 Words
    • 4 Pages

    Complete the University of Phoenix Material: File Processing Commands Worksheet located on your student website.…

    • 583 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    It210 Appendix F

    • 372 Words
    • 3 Pages

    1. The program will present a series of user screens that prompts the user for specified input.…

    • 372 Words
    • 3 Pages
    Satisfactory Essays
  • Good Essays

    Assignment BBAC 101 2 1

    • 722 Words
    • 9 Pages

    Your submission must be in the form of one single file to be uploaded into the system. The file should be a Word Document (.doc or .docx). PDF files which have been created by scanning a printed copy of your assignment will not be accepted.…

    • 722 Words
    • 9 Pages
    Good Essays
  • Good Essays

    To save this document so that you can type in it and save your work:…

    • 1579 Words
    • 7 Pages
    Good Essays
  • Good Essays

    COS wa 10

    • 297 Words
    • 2 Pages

    Create a set/list of enumerated constants called week that contains the days of the week. Have a variable called today that is of type week. Assign a value to today. If the day is Monday through Friday, print "Go to work!" If the day is Saturday or Sunday, print "You can rest today!"…

    • 297 Words
    • 2 Pages
    Good Essays
  • Powerful Essays

    what ever

    • 1175 Words
    • 5 Pages

    Be sure your output file contains user prompts and what was entered by the user .in addition to the results of your program processing. Run with above listed data. The instructor will give you additional data to test.…

    • 1175 Words
    • 5 Pages
    Powerful Essays
  • Satisfactory Essays

    Beginner

    • 466 Words
    • 2 Pages

    Following are sample answers to these questions. Your answers will be different because your filesystems are different.…

    • 466 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT STUDENT-FILE ASSIGN TO STUDENT. SELECT STUDENT-REPORT ASSIGN TO STDREPT. DATA DIVISION. FILE SECTION. FD STUDENT-FILE LABEL RECORDS ARE OMITTED RECORD CONTAINS 80 CHARACTERS RECORDING MODE IS F. 01 STUDENT-REC-IN.…

    • 756 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    package lab4.t1; import java.net. *; import java.io. *; public class Lab4T1 public static void main(String[] args) throws Exception { { URL aURL = new URL("http://example.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost()); System.out.println("port = " + aURL.getPort()); System.out.println("path = " + aURL.getPath()); System.out.println("query = "…

    • 310 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    Java Iq Test Program

    • 2251 Words
    • 10 Pages

    System.out.println("\nThere Are Seven Seniors On The Road\nEach One Having The Seven Crutches.\nHow Many Crutches Are At All?");…

    • 2251 Words
    • 10 Pages
    Good Essays
  • Satisfactory Essays

    Java Native Access

    • 266 Words
    • 2 Pages

    Java Native Access import com.sun.jna. Library; import com.sun.jna. Native; import com.sun.jna. Platform; / *…

    • 266 Words
    • 2 Pages
    Satisfactory Essays

Related Topics