Question

Using JAVA For this assignment, you will analyze code that uses a file input stream and...

Using JAVA

For this assignment, you will analyze code that uses a file input stream and a file output stream.

Read through the linked Java™ code.

In a Microsoft® Word document, answer the following questions:

  • Could this program be run as is? If not, what is it lacking?
  • Does this program modify the contents of an input stream? In what way?
  • What are the results of running this code?

********************************************** CODE TO ANALYZE  ********************************************************

/**********************************************************************
*   Program:   Datasort
*   Purpose:    Java code that sorts, extracts data and saves it to a collection
*   Programmer:   I am student          
*   Class:       PRG/421r13, Java Programming II          
*   Instructor:              
*   Creation Date:   12/01/2017  
*
* Comments:
* Extracts data from a file, sorts it, displays it onscreen, and saves it.
*
***********************************************************************/

// import the needed classes

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;


public class Datasort {

public static void main (String [] args) throws IOException {

File fin = new File("e:\\input.txt");   // input file on e: drive (with data)   
File fout = new File("e:\\sorted.txt");   // create an out file on e: drive

// Java FileInputStream class obtains input bytes from a file
FileInputStream fis = new FileInputStream(fin);  
FileOutputStream fos = new FileOutputStream(fout);

// buffering characters so as to provide for the efficient reading of characters, arrays, and lines
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos));

// declare an array in-line, ready for the sort

String aLine;
ArrayList<String> al = new ArrayList<String> ();

int i = 0;
while ((aLine = in.readLine()) != null) {
// set the sort for values is greater than 0
   if (!aLine.trim().startsWith("-") && aLine.trim().length() > 0) {
       al.add(aLine);
       i++;
           }
       }

Collections.sort(al);   // sorted content to the output file
for (String s : al) {
System.out.println(s);
   out.write(s);
   out.newLine();
   out.newLine();
   }
// close the 2 files
       in.close();
       out.close();
   }
}

Homework Answers

Answer #1

1)

Ans:- This program runs and executed successfully but only works for string data not for integer data.

for example file contains only integer data then

/**********input.txt**************/

12
132
0
34
-5
7
344
654
98


/**********************output*********************/

0
12
132
34
344
654
7
98

Ans:- it will ignore the line in the file that length is zero and start with "-" character. if line contains extra space then trim the spaces of the line and put it into the array list of string type and sort it.

Q3)

/*********************input.txt***********************/

Jugal Kishor
Virat Kohli
MS Dhoni
-JKS
Thanks

/*********************output************************/

Jugal Kishor
MS Dhoni
Thanks
Virat Kohli

Please let me know if you have any doubt or modify the answer, Thanks :)

Know the answer?
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
Similar Questions
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Write a java program that calculates the number of duplicate words in a sentence. You may...
Write a java program that calculates the number of duplicate words in a sentence. You may assume that the input consists only 52 English characters with a single white-space as the separator between words (not punctuations in the inputted sentence). You need consider uppercase and lowercase letters the same. You may get the input from the keyboard. (You need to use Set and/or Hashset). What I have so far import java.util.HashSet; import java.util.Scanner; public class Duplicate { int counter; int...
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
using java LO: (Remember) Students will recall how to read from standard input. LO: (Apply) Students...
using java LO: (Remember) Students will recall how to read from standard input. LO: (Apply) Students will write loops that iterate as long as a condition is true. Write a program that reads words from standard input and displays them to standard output. When the word "end" is read, print it out and then stop reading input. starter code: import java.util.Scanner; /* * Reads and displays words from standard input until hitting a stop word. */ public class WhileLoop {...
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...