Archive
Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream – Q35
Q35: Java Program to read data from file through inputfilestream and write its contents on another file through fileoutputstream.
import java.io.*;
import javax.swing.*;
class File_RW{
public static void main(String args[]) throws IOException{
String f_name = JOptionPane.showInputDialog("Enter a file name: ");
InputStream infile = new FileInputStream(f_name);
int fsize = infile.available();
f_name = JOptionPane.showInputDialog("Enter a file name to Save As: ");
OutputStream outfile = new FileOutputStream(f_name, true);
System.out.println("Reading File : " + f_name + "\n");
char ch;
for(int i=0; i<fsize; i++){
ch = (char)infile.read();
outfile.write(ch);
System.out.print(ch);
}
infile.close();
outfile.close();
System.out.println("File Successfully Copied.");
}
}
… from College notes (BCA/MCA assignments):
Java Program to read data from a file through inputfilestream and show it on the console. – Q34
Q34: Java Program to read data from a file through inputfilestream and show it on the console.
import java.io.*;
import javax.swing.*;
class File_read{
public static void main(String args[]) throws IOException{
String f_name = JOptionPane.showInputDialog("Enter a File name: ");
File f = new File(f_name);
InputStream file = new FileInputStream(f);
int fsize = file.available();
System.out.println("Reading File : " + f.getName() + "\n");
for(int i=0; i<fsize; i++)
System.out.print((char)file.read());
}
}
… from College notes (BCA/MCA assignments):
Java Program to print all the directories, files within those directories and files in a particular directory – Q33
Q33: Java Program to print all the directories, files within those directories and files in a particular directory.
import java.io.File;
import javax.swing.*;
class DirList{
public static void main(String args[]){
String dirname = JOptionPane.showInputDialog("Enter a directory: ");
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");
}
}
}
… from College notes (BCA/MCA assignments):
Java Program to display file attributes like isfile(), exist() etc – Q32
Q32: Java Program to display file attributes like isfile(), exist() etc
import java.io.*;
class file_attrib{
static void p(String s){
System.out.println(s);
}
public static void main(String args[]){
File f1 = new File("fact_rec.java");
p("File name: " + f1.getName());
p("path: " + f1.getPath());
p("Abs Path :" + f1.getAbsolutePath());
p("Parent: "+ f1.getParent());
p(f1.exists() ? "exists" : "dose not exist");
p(f1.canWrite() ? "is writable" : "is not writable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? "" : "not a directory"));
p(f1.isFile() ? "is normal file" : "might be a named pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified: " + f1.lastModified());
p("File size: " + f1.length() + " Bytes");
}
}
… from College notes (BCA/MCA assignments):
Java Program to show Thread Synchronization by user defined Get & Put methods – Q31
Q31: Java Program to create Q class having Get and Put methods and then create Producer and Consumer classes which accept Q object in their constructor. Put and get the numbers through a for loop in producer and consumer, use an array to put and get the numbers.
class Q{
int n;
synchronized int get(){
System.out.println("Get: " + n);
return n;
}
synchronized void put(int n){
this. n = n;
System.out.println("Put: " + n);
}
}
class Producer implements Runnable{
Q q;
Producer(Q q){
this.q = q;
new Thread(this, "Producer").start();
}
public void run(){
int i=0;
while(true){
q.put(i++);
}
}
}
class Consumer implements Runnable{
Q q;
Consumer(Q q){
this.q = q;
new Thread(this, "Consumer").start();
}
public void run(){
while(true){
q.get();
}
}
}
class PC{
public static void main(String args[]){
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Ctrl-C to STOP");
}
}
… from College notes (BCA/MCA assignments):




