Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array – Q37
Q37: Java Program to write the contents onto a ByteArrayOutputStream and from there write the contents on a file, and on another byte array.
import java.io.*;
import javax.swing.*;
class B_a_o_s{
public static void main(String args[]) throws IOException{
String s = JOptionPane.showInputDialog("Enter a Sentence:
");
ByteArrayOutputStream f = new ByteArrayOutputStream();
byte buff[] = s.getBytes();
f.write(buff);
System.out.println("Buffer as a String");
System.out.println(f.toString());
System.out.println("Into Array");
byte b[] = f.toByteArray();
for(int i=0; i<b.length; i++)
System.out.print((char) b[i]);
System.out.println("\n To an output Stream");
OutputStream f2 = new FileOutputStream("test.txt");
f.writeTo(f2);
f2.close();
f.reset();
for(int i=0; i<3; i++)
f.write('X');
System.out.println(f.toString());
}
}
… from College notes (BCA/MCA assignments):
Java Program to read from byte array using ByteArrayInputStream and print the characters onto console – Q36
Q36: Java Program to read from byte array using ByteArrayInputStream and print the characters onto console.
import java.io.*;
import javax.swing.*;
class B_a_i_s{
public static void main(String args[]){
String inp = JOptionPane.showInputDialog("Enter a String:");
byte b_arr[] = inp.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(b_arr);
int c;
while((c = in.read()) != -1){
System.out.print((char)c);
}
}
}
… from College notes (BCA/MCA assignments):
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):




