class WriteDemo
{
public static void main(String args[])
{
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}
write a programe that create a file and takes its inputs as one line at a time and put one charecter at a time
import java.io.*;
class CreatingFileAsLine
{
public static void main(String args[]) throws IOException
{
int i;
FileOutputStream fout;
String s;
char c[];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter line, 'stop' to quit.");
try {
// open output file
fout = new FileOutputStream("D:\\Book\\a.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Error Opening Output File");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: CopyFile From To");
return;
}
try {
do {
s = (String) br.readLine();
c=new char[s.length()];
s.getChars(0,s.length(),c,0);
for(i=0;i<s.length();i++)
fout.write(c[i]);
} while(!s.equals("stop"));
} catch(IOException e) {
System.out.println("File Error");
}
fout.close();
}
}
class CreatingFileAsLine
{
public static void main(String args[]) throws IOException
{
int i;
FileOutputStream fout;
String s;
char c[];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter line, 'stop' to quit.");
try {
// open output file
fout = new FileOutputStream("D:\\Book\\a.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Error Opening Output File");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: CopyFile From To");
return;
}
try {
do {
s = (String) br.readLine();
c=new char[s.length()];
s.getChars(0,s.length(),c,0);
for(i=0;i<s.length();i++)
fout.write(c[i]);
} while(!s.equals("stop"));
} catch(IOException e) {
System.out.println("File Error");
}
fout.close();
}
}
write a programe that create a file and takes its inputs as one charecer at a time
import java.io.*;
class CreatingFile
{
public static void main(String args[]) throws IOException
{
int i;
FileOutputStream fout;
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
try {
// open output file
fout = new FileOutputStream("D:\\Book\\a.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Error Opening Output File");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: CopyFile From To");
return;
}
try {
do {
c = (char) br.read();
fout.write(c);
} while(c != 'q');
} catch(IOException e) {
System.out.println("File Error");
}
fout.close();
}
}
class CreatingFile
{
public static void main(String args[]) throws IOException
{
int i;
FileOutputStream fout;
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
try {
// open output file
fout = new FileOutputStream("D:\\Book\\a.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Error Opening Output File");
return;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Usage: CopyFile From To");
return;
}
try {
do {
c = (char) br.read();
fout.write(c);
} while(c != 'q');
} catch(IOException e) {
System.out.println("File Error");
}
fout.close();
}
}
Write a programe that Reads a string from console using a BufferedReader.
import java.io.*;
class BRReadLines
{
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do
{
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}
class BRReadLines
{
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do
{
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}
Write a programe use a BufferedReader to read characters from the console.
import java.io.*;
class BRRead
{
public static void main(String args[])
throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do
{
c = (char) br.read();
System.out.print(c);
} while(c != 'q');
}
}
class BRRead
{
public static void main(String args[])
throws IOException
{
char c;
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter characters, 'q' to quit.");
// read characters
do
{
c = (char) br.read();
System.out.print(c);
} while(c != 'q');
}
}
Write a programe that shows names of the HTML files wihin a filepath.
import java.io.*;
class OnlyExt1 implements FilenameFilter
{
String ext;
public OnlyExt1(String ext)
{
this.ext = "." + ext;
}
public boolean accept(File dir, String name)
{
return name.endsWith(ext);
}
}
class DirListOnly
{
public static void main(String args[])
{
String dirname = "C:\\Program Files\\Java\\jre1.6.0";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt1("html");
String s[] = f1.list(only);
for (int i=0; i < s.length; i++)
{
System.out.println(s[i]);
}
}
}
class OnlyExt1 implements FilenameFilter
{
String ext;
public OnlyExt1(String ext)
{
this.ext = "." + ext;
}
public boolean accept(File dir, String name)
{
return name.endsWith(ext);
}
}
class DirListOnly
{
public static void main(String args[])
{
String dirname = "C:\\Program Files\\Java\\jre1.6.0";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt1("html");
String s[] = f1.list(only);
for (int i=0; i < s.length; i++)
{
System.out.println(s[i]);
}
}
}
Write a programe that illustrates how to use isDirectory() and list() methods to examine the contents of a directiry.
import java.io.File;
class DirList
{
public static void main(String args[])
{
String dirname = "/java";
File f1 = new File(dirname);
if (f1.isDirectory())
{
System.out.println("Directory of " + dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++)
{
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory())
{
System.out.println(s[i] + " is a directory");
}
else
{
System.out.println(s[i] + " is a file");
}
}
}
else
{
System.out.println(dirname + " is not a directory");
}
}
}
class DirList
{
public static void main(String args[])
{
String dirname = "/java";
File f1 = new File(dirname);
if (f1.isDirectory())
{
System.out.println("Directory of " + dirname);
String s[] = f1.list();
for (int i=0; i < s.length; i++)
{
File f = new File(dirname + "/" + s[i]);
if (f.isDirectory())
{
System.out.println(s[i] + " is a directory");
}
else
{
System.out.println(s[i] + " is a file");
}
}
}
else
{
System.out.println(dirname + " is not a directory");
}
}
}
Subscribe to:
Posts (Atom)