import java.io.*;
class TinyEdit
{
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str[] = new String[100];
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
for(int i=0; i<100; i++)
{
str[i] = br.readLine();
if(str[i].equals("stop"))
break;
}
System.out.println("\nHere is your file:");
// display the lines
for(int i=0; i<100; i++)
{
if(str[i].equals("stop"))
break;
System.out.println(str[i]);
}
}
}
Java Programming Source Code
This is very helpful site for Computer programs related workers.
Write a programe that create files using FileWriter and also use different form write method.
import java.io.*;
class FileWriterDemo
{
public static void main(String args[]) throws Exception
{
String source = "Now is the time for all good men\n"
+ " to come to the aid of their country\n"
+ " and pay their due taxes.";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f0 = new FileWriter("file1.txt");
for (int i=0; i < buffer.length; i += 2)
f0.write(buffer[i]);
f0.close();
FileWriter f1 = new FileWriter("file2.txt");
f1.write(buffer);
f1.close();
FileWriter f2 = new FileWriter("file3.txt");
f2.write(buffer,buffer.length-buffer.length/4,buffer.length/4);
f2.close();
}
}
class FileWriterDemo
{
public static void main(String args[]) throws Exception
{
String source = "Now is the time for all good men\n"
+ " to come to the aid of their country\n"
+ " and pay their due taxes.";
char buffer[] = new char[source.length()];
source.getChars(0, source.length(), buffer, 0);
FileWriter f0 = new FileWriter("file1.txt");
for (int i=0; i < buffer.length; i += 2)
f0.write(buffer[i]);
f0.close();
FileWriter f1 = new FileWriter("file2.txt");
f1.write(buffer);
f1.close();
FileWriter f2 = new FileWriter("file3.txt");
f2.write(buffer,buffer.length-buffer.length/4,buffer.length/4);
f2.close();
}
}
Write a java programe that reads itself as one charecter at a time using FileReader class.
import java.io.*;
class FileReaderDemo1
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
int s;
while((s = br.read()) != -1)
{
System.out.print((char)s);
}
fr.close();
}
}
class FileReaderDemo1
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
int s;
while((s = br.read()) != -1)
{
System.out.print((char)s);
}
fr.close();
}
}
Write a java programe that reads itself as one line at a time.
import java.io.*;
class FileReaderDemo
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null)
{
System.out.println(s);
}
fr.close();
}
}
class FileReaderDemo
{
public static void main(String args[]) throws Exception
{
FileReader fr = new FileReader("FileReaderDemo.java");
BufferedReader br = new BufferedReader(fr);
String s;
while((s = br.readLine()) != null)
{
System.out.println(s);
}
fr.close();
}
}
Read a file as charecter and create another file that contains the contents of privious file.
import java.io.*;
class CopyFile
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try {
// open input file
try {
fin = new FileInputStream("D:\\Book\\a.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Input File Not Found");
return;
}
// open output file
try {
fout = new FileOutputStream("D:\\Book\\b.txt");
} catch(FileNotFoundException e)
{
System.out.println("Error Opening Output File");
return;
}
} catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
}
catch(IOException e)
{
System.out.println("File Error");
}
fin.close();
fout.close();
}
}
class CopyFile
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try {
// open input file
try {
fin = new FileInputStream("D:\\Book\\a.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Input File Not Found");
return;
}
// open output file
try {
fout = new FileOutputStream("D:\\Book\\b.txt");
} catch(FileNotFoundException e)
{
System.out.println("Error Opening Output File");
return;
}
} catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
}
catch(IOException e)
{
System.out.println("File Error");
}
fin.close();
fout.close();
}
}
Write a java programe that reads itself and display on output.
import java.io.*;
class ShowFile
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try
{
fin = new FileInputStream("ShowFile.java");
} catch(FileNotFoundException e)
{
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do
{
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
class ShowFile
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try
{
fin = new FileInputStream("ShowFile.java");
} catch(FileNotFoundException e)
{
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do
{
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
Write a java programe that reads a file as one charecter at a time with espacific directory and display on output.
import java.io.*;
class ShowFile1
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try
{
fin = new FileInputStream("D:\\Book\\a.txt");
} catch(FileNotFoundException e)
{
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do
{
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
class ShowFile1
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin;
try
{
fin = new FileInputStream("D:\\Book\\a.txt");
} catch(FileNotFoundException e)
{
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: ShowFile File");
return;
}
// read characters until EOF is encountered
do
{
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}
Subscribe to:
Posts (Atom)