By: Ganesh Iyer in Java Tutorials on 2009年06月03日 [フレーム]
In this tutorial I will explain about File I/O functionality concept in JAVA. In this example, I have a file named compare.java which I want to write to a file named out.txt with the line numbers prefixed to each line.
/* Author - Ganesh Iyer Contact No (0)9176148207
Program -
Written on - 03-06-2009 */
import java.io.*;
class Finputoutput{
public static void main(String[] args) throws Exception
{
//Check for two arguments
if(args.length != 2)
{
System.out.println(\"To execute the program
provide two parameters <INPUT> <SPACE> <OUTPUT> Files
\");
System.exit(1);
}
String fname = args[0], wname = args[1], str;
int counter=0;
FileReader f = new FileReader(fname);
BufferedReader in = new BufferedReader(f);
FileWriter fw = new FileWriter(wname);
while ((str=in.readLine()) != null)
{
counter++;
fw.write(counter+\"
\"+str+\"\\n\");
System.out.println(counter+\"
\"+str);
}
f.close();
fw.close();
}
}
To Execute - java Finputoutput <INPUT> <SPACE> <OUTPUT> Example - java Finputoutput compare.java out.txt
Output
1 class compare
2 {
3 public static void main(String[] args)
4 {
5 numcompare comparenum = new numcompare();
6 comparenum.threenumcompare();
7 }
8 }
9
10 class numcompare
11 {
12 void threenumcompare()
13 {
14
15 int a = 1;
16 int b = 2;
17 int c = 3;
18
19 if ((a>b)&&(c<a))
20 {
21 System.out.println(\"a
is gt b&c\");
22 }
23 else if ((b>a) &&
(c<b))
24 {
25 System.out.println(\"b
is gt a&c\");
26 }
27 else
28 {
29 System.out.println(\"c
is gt a&b\");
30 }
31 }
32 }
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Latest Articles (in Java)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate