0% found this document useful (0 votes)
3 views11 pages

Java Week 8

The document outlines three programming experiments in Java, focusing on file handling and data processing. Experiment 9 involves creating programs to analyze file contents, calculate supermarket revenue from a CSV file, and extract and sort technical terms from a text file while excluding stop words. Each experiment includes code examples, input/output scenarios, and screenshots of console outputs.

Uploaded by

dtanishsai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Java Week 8

The document outlines three programming experiments in Java, focusing on file handling and data processing. Experiment 9 involves creating programs to analyze file contents, calculate supermarket revenue from a CSV file, and extract and sort technical terms from a text file while excluding stop words. Each experiment includes code examples, input/output scenarios, and screenshots of console outputs.

Uploaded by

dtanishsai
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

19131A0531 OOP Through JAVA Lab

Experiment 9:
a) Write a program that reads file name from the user then displays information about that file, also
read the contents from the file in byte stream to count number of alphabets, numeric values and
special symbols. Write these statistics into another file using byte streams

[Link]:
import [Link].*;
import [Link];
class CountChar31
{
public static void main(String arg[]) throws IOException
{
try{
Scanner s=new Scanner([Link]);
[Link]("Enter the file name: ");
String fname=[Link]();
File f=new File(fname);

if([Link]()) [Link]("File Found"); else [Link]("File Not Found");


[Link]("Name: "+[Link]());
[Link]("Path: "+[Link]());
[Link]("Absolute Path: "+[Link]());
[Link]("Parent: "+[Link]());
[Link]("Size: "+[Link]());

FileInputStream in=new FileInputStream(f);


FileOutputStream out=new FileOutputStream("[Link]");
int i,alpha=0,num=0,specsym=0;
while((i=[Link]())!=-1)
{
char c=(char)i;
if( (c>='A' && c<='Z') || (c>='a' && c<='z') )
alpha+=1;
else if( c>='0' && c<='9' )
num+=1;
else if( c!=' ' && c!='.' && c!='\n' )
specsym+=1;
[Link](c);
}

String str=new String("\nAlphabets: "+alpha+"\nNumbers: "+num+"\nSpecial Symbols: "+specsym);


byte b[]=[Link]();
[Link](b);

[Link]();
[Link]();
}
catch (FileNotFoundException e) { [Link]("Exception: "+e); }
}
}

1
19131A0531 OOP Through JAVA Lab

[Link]:
Hello @ll. I am #Sumanth. Roll 19131A0531.

Input:
Enter the file name: [Link]

Output:
File Found
Name: [Link]
Path: [Link]
Absolute Path: D:\OneDrive\SUMANTH\ENGINEERING\Semester 4\OOP through JAVA\Lab\9\[Link]
Parent: null
Size: 42

After the output, [Link]:


Hello @ll. I am #Sumanth. Roll 19131A0531.
Alphabets: 22
Numbers: 9
Special Symbols: 2

Input:
Enter the file name: [Link]

Output:
File Not Found
Name: [Link]
Path: [Link]
Absolute Path: D:\OneDrive\SUMANTH\ENGINEERING\Semester 4\OOP through JAVA\Lab\9\[Link]
Parent: null
Size: 0
Exception: [Link]: [Link] (The system cannot find the file specified)

Screenshots:
Console Output:

2
19131A0531 OOP Through JAVA Lab

Before Execution:
Only “[Link]” file was located in the local folder.

After Execution:
New file named as “[Link]” has been created and below shown data has been stored into it.

3
19131A0531 OOP Through JAVA Lab

b) Write a program that reads a CSV file containing a super market data containing product ID, Name,
Cost and Quantity of sales and calculate the total revenue of the super market also sort the products
in the order of their demand

[Link]:
import [Link];
import [Link].*;
class demand31{
public static void main(String arg[]) throws FileNotFoundException, IOException
{
Scanner s=new Scanner(new File("[Link]"));
int total_revenue=0,i=0;
String[][] products=new String[10][4];
while([Link]()) {
products[i]=[Link]().split(",");
total_revenue+=[Link](products[i][2])*[Link](products[i][3]);
i+=1;
}
[Link]("Total revenue of the Super Market is: "+total_revenue);
for(int x=0;x<i;x++){
for(int y=x+1;y<i;y++){
if([Link](products[y][3])>[Link](products[x][3])){
String temp[]=products[y];
products[y]=products[x];
products[x]=temp;
}
}
}
FileOutputStream out=new FileOutputStream("[Link]");
for(int x=0;x<i;x++){
for(int y=0;y<4;y++) { [Link]((products[x][y]+",").getBytes()); }
[Link]("\n".getBytes()); }
}
}

[Link]:
99,Rice,1200,40
156,Pulses,70,90
31,Chocolate,20,100
100,Coke,50,20
1,Soap,40,30

Output:
Total revenue of the Super Market is: 58500

After Output, [Link]:


31,Chocolate,20,100,
156,Pulses,70,90,
99,Rice,1200,40,
1,Soap,40,30,
100,Coke,50,20,

4
19131A0531 OOP Through JAVA Lab

Screenshots:
Before Execution:
A file named “[Link]” was located in the local folder which containing the data shown below

Console Output:

5
19131A0531 OOP Through JAVA Lab

After Execution:
Data in the file “[Link]” has been sorted based on their demands

6
19131A0531 OOP Through JAVA Lab

c) Write a program that reads a text file containing some technical content and identify the technical
terms and sort them alphabetically. Note: use a file containing stop words (general English and
Grammar terms as many terms as possible)

[Link]:
import [Link].*;
import [Link].*;
class techTerms31{
public static void main(String arg[]) throws IOException,FileNotFoundException
{
Scanner s=new Scanner([Link]);
[Link]("Enter the file name containing technical content: ");
File techFile=new File([Link]());
[Link]("Enter the file name containing stop words: ");
File stopWordsFile=new File([Link]());

BufferedReader f1=new BufferedReader(new FileReader(techFile));


BufferedReader f2=new BufferedReader(new FileReader(stopWordsFile));

ArrayList<String> stopWords=new ArrayList<String>();


ArrayList<String> techWords=new ArrayList<String>();

String line;
while((line=[Link]()) != null)
{ for(String x:[Link](" ")){ [Link](x); } }

while((line=[Link]()) !=null)
{ for(String x:[Link](" ")){ if(![Link](x)) [Link](x); } }

[Link]();
[Link]();

[Link](techWords);
FileWriter fw=new FileWriter(techFile);
for(String word:techWords) { [Link](word+"\n"); }

[Link]();

}
}

Input:
Enter the file name containing technical content: [Link]
Enter the file name containing stop words: [Link]

7
19131A0531 OOP Through JAVA Lab

[Link]:
Java provides a number of classes and methods that allow you to read and write files. Before we begin,
it is important to state that the topic of file I/O is quite large and file I/O is examined in detail in Part II.
The purpose of this section is to introduce the basic techniques that read from and write to a file.
Although byte streams are used, these techniques can be adapted to the character-based streams.
Two of the most often-used stream classes are FileInputStream and FileOutputStream, which create
byte streams linked to files. To open a file, you simply create an object of one of these classes,
specifying the name of the file as an argument to the constructor. Although both classes support
additional constructors, the following are the forms that we will be using:
FileInputStream(String fileName) throws FileNotFoundException
FileOutputStream(String fileName) throws FileNotFoundException
Here, fileName specifies the name of the file that you want to open. When you create an input stream,
if the file does not exist, then FileNotFoundException is thrown. For output streams, if the file cannot
be opened or created, then FileNotFoundException is thrown. FileNotFoundException is a subclass of
IOException. When an output file is opened, any preexisting file by the same name is destroyed.

After Execution, [Link]:


Although
Although
Before
FileInputStream
FileInputStream(String
FileNotFoundException
FileNotFoundException
FileNotFoundException
FileNotFoundException
FileNotFoundException
FileOutputStream(String
FileOutputStream,
For
Here,
I/O
I/O
II.
IOException.
Java
Part
The
To
Two
When
When
adapted
additional
allow
argument
basic
begin,
byte
byte
character-based

8
19131A0531 OOP Through JAVA Lab

classes
classes
classes
classes,
constructor.
constructors,
create
create
create
created,
destroyed.
detail
examined
exist,
file
file
file
file
file
file
file
file
file,
file.
fileName
fileName)
fileName)
files.
files.
following
forms
input
introduce
linked
methods
name
name
name
object
often-used
open.
opened,
output
output
preexisting
provides
purpose
read
read
section
simply

9
19131A0531 OOP Through JAVA Lab

specifies
specifying
stream
stream,
streams
streams
streams,
streams.
subclass
support
techniques
techniques
thrown.
thrown.
throws
throws
topic
used,
using:
write
write

Screenshots:
Before Execution:
Local folder consisting of “[Link]” containing the technical content and “[Link]”
containing the list of stop words

10
19131A0531 OOP Through JAVA Lab

Console Output:

After Execution:
Local folder consisting of “[Link]” file consisting of list of sorted technical words extracted
from previous content in the file

11

You might also like