import java.io.*; import com.elharo.io.*; public class FileDumper { public static final int ASC = 0; public static final int DEC = 1; public static final int HEX = 2; public static void main(String[] args) { if (args.length < 1) { System.err.println("Usage: java FileDumper [-ahd] file1 file2..."); return; } int firstArg = 0; int mode = ASC; if (args[0].startsWith("-")) { firstArg = 1; if (args[0].equals("-h")) mode = HEX; else if (args[0].equals("-d")) mode = DEC; } for (int i = firstArg; i < args.length; i++) { try { if (mode == ASC) dumpAscii(args[i]); else if (mode == HEX) dumpHex(args[i]); else if (mode == DEC) dumpDecimal(args[i]); } catch (IOException ex) { System.err.println("Error reading from " + args[i] + ": " + ex.getMessage()); } if (i < args.length-1) { // more files to dump System.out.println("\r\n--------------------------------------\r\n"); } } } public static void dumpAscii(String filename) throws IOException { FileInputStream fin = null; try { fin = new FileInputStream(filename); StreamCopier.copy(fin, System.out); } finally { if (fin != null) fin.close(); } } public static void dumpDecimal(String filename) throws IOException { FileInputStream fin = null; byte[] buffer = new byte[16]; boolean end = false; try { fin = new FileInputStream(filename); while (!end) { int bytesRead = 0; while (bytesRead < buffer.length) { int r = fin.read(buffer, bytesRead, buffer.length - bytesRead); if (r == -1) { end = true; break; } bytesRead += r; } for (int i = 0; i < bytesRead; i++) { int dec = buffer[i]; if (dec < 0) dec = 256 + dec; if (dec < 10) System.out.print("00" + dec + " "); else if (dec < 100) System.out.print("0" + dec + " "); else System.out.print(dec + " "); } System.out.println(); } } finally { if (fin != null) fin.close(); } } public static void dumpHex(String filename) throws IOException { FileInputStream fin = null; byte[] buffer = new byte[24]; boolean end = false; try { fin = new FileInputStream(filename); while (!end) { int bytesRead = 0; while (bytesRead < buffer.length) { int r = fin.read(buffer, bytesRead, buffer.length - bytesRead); if (r == -1) { end = true; break; } bytesRead += r; } for (int i = 0; i < bytesRead; i++) { int hex = buffer[i]; if (hex < 0) hex = 256 + hex; if (hex >= 16) System.out.print(Integer.toHexString(hex) + " "); else System.out.print("0" + Integer.toHexString(hex) + " "); } System.out.println(); } } finally { if (fin != null) fin.close(); } } }
FileDumper Class in java
FileDumper Class in java
FileCopier Class in java
FileCopier Class in java
import java.io.*; import com.elharo.io.*; public class FileCopier { public static void main(String[] args) { if (args.length != 2) { System.err.println("Usage: java FileCopier infile outfile"); } try { copy(args[0], args[1]); } catch (IOException ex) { System.err.println(ex); } } public static void copy(String inFile, String outFile) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; try { fin = new FileInputStream(inFile); fout = new FileOutputStream(outFile); StreamCopier.copy(fin, fout); } finally { try { if (fin != null) fin.close(); } catch (IOException ex) { } try { if (fout != null) fout.close(); } catch (IOException ex) { } } } }
FileTyper class in java
FileTyper class in java
import java.io.*; import com.elharo.io.*; public class FileTyper { public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Usage: java FileTyper filename"); return; } typeFile(args[0]); } public static void typeFile(String filename) throws IOException { FileInputStream fin = new FileInputStream(filename); try { StreamCopier.copy(fin, System.out); } finally { fin.close(); } } }
Program using NullOutputStream Class in java
Program using NullOutputStream Class in java
package com.elharo.io;
import java.io.*;
public class NullOutputStream extends OutputStream {
private boolean closed = false;
public void write(int b) throws IOException {
if (closed) throw new IOException("Write to closed stream");
}
public void write(byte[] data, int offset, int length)
throws IOException {
if (data == null) throw new NullPointerException("data is null");
if (closed) throw new IOException("Write to closed stream");
}
public void close() {
closed = true;
}
}
package com.elharo.io;
import java.io.*;
public class NullOutputStream extends OutputStream {
private boolean closed = false;
public void write(int b) throws IOException {
if (closed) throw new IOException("Write to closed stream");
}
public void write(byte[] data, int offset, int length)
throws IOException {
if (data == null) throw new NullPointerException("data is null");
if (closed) throw new IOException("Write to closed stream");
}
public void close() {
closed = true;
}
}
Java Program to show AsciiArray class
Java Program to show AsciiArray class
import java.io.*;
public class AsciiArray {
public static void main(String[] args) {
byte[] b = new byte[(127-31)*2];
int index = 0;
for (int i = 32; i < 127; i++) {
b[index++] = (byte) i;
// Break line after every eight characters.
if (i % 8 == 7) b[index++] = (byte) '\n';
else b[index++] = (byte) '\t';
}
b[index++] = (byte) '\n';
try {
System.out.write(b);
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
import java.io.*;
public class AsciiArray {
public static void main(String[] args) {
byte[] b = new byte[(127-31)*2];
int index = 0;
for (int i = 32; i < 127; i++) {
b[index++] = (byte) i;
// Break line after every eight characters.
if (i % 8 == 7) b[index++] = (byte) '\n';
else b[index++] = (byte) '\t';
}
b[index++] = (byte) '\n';
try {
System.out.write(b);
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
StreamCopier class in Java
StreamCopier class in Java
package com.elharo.io;
import java.io.*;
public class StreamCopier {
public static void main(String[] args) {
try {
copy(System.in, System.out);
}
catch (IOException ex) {
System.err.println(ex);
}
}
public static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
package com.elharo.io;
import java.io.*;
public class StreamCopier {
public static void main(String[] args) {
try {
copy(System.in, System.out);
}
catch (IOException ex) {
System.err.println(ex);
}
}
public static void copy(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
}
}
}
Program for RandomInputStream Class in Java
Program for RandomInputStream Class in Java
package com.elharo.io;
import java.util.*;
import java.io.*;
public class RandomInputStream extends InputStream {
private Random generator = new Random();
private boolean closed = false;
public int read() throws IOException {
checkOpen();
int result = generator.nextInt() % 256;
if (result < 0) result = -result;
return result;
}
public int read(byte[] data, int offset, int length) throws IOException {
checkOpen();
byte[] temp = new byte[length];
generator.nextBytes(temp);
System.arraycopy(temp, 0, data, offset, length);
return length;
}
public int read(byte[] data) throws IOException {
checkOpen();
generator.nextBytes(data);
return data.length;
}
public long skip(long bytesToSkip) throws IOException {
checkOpen();
// It's all random so skipping has no effect.
return bytesToSkip;
}
public void close() {
this.closed = true;
}
private void checkOpen() throws IOException {
if (closed) throw new IOException("Input stream closed");
}
public int available() {
// Limited only by available memory and the size of an array.
return Integer.MAX_VALUE;
}
}
package com.elharo.io;
import java.util.*;
import java.io.*;
public class RandomInputStream extends InputStream {
private Random generator = new Random();
private boolean closed = false;
public int read() throws IOException {
checkOpen();
int result = generator.nextInt() % 256;
if (result < 0) result = -result;
return result;
}
public int read(byte[] data, int offset, int length) throws IOException {
checkOpen();
byte[] temp = new byte[length];
generator.nextBytes(temp);
System.arraycopy(temp, 0, data, offset, length);
return length;
}
public int read(byte[] data) throws IOException {
checkOpen();
generator.nextBytes(data);
return data.length;
}
public long skip(long bytesToSkip) throws IOException {
checkOpen();
// It's all random so skipping has no effect.
return bytesToSkip;
}
public void close() {
this.closed = true;
}
private void checkOpen() throws IOException {
if (closed) throw new IOException("Input stream closed");
}
public int available() {
// Limited only by available memory and the size of an array.
return Integer.MAX_VALUE;
}
}
StreamPrinter Class in Java
StreamPrinter Class in Java
import java.io.*;
public class StreamPrinter {
public static void main(String[] args) {
try {
while (true) {
int datum = System.in.read();
if (datum == -1) break;
System.out.println(datum);
}
}
catch (IOException ex) {
System.err.println("Couldn't read from System.in!");
}
}
}
import java.io.*;
public class StreamPrinter {
public static void main(String[] args) {
try {
while (true) {
int datum = System.in.read();
if (datum == -1) break;
System.out.println(datum);
}
}
catch (IOException ex) {
System.err.println("Couldn't read from System.in!");
}
}
}
Program to Print ASCII values in Java
Program to Print ASCII values in Java
import java.io.*;
public class AsciiChart
{
public static void main(String[] args) {
for (int i = 32; i < 127; i++) {
System.out.write(i);
// break line after every eight characters.
if (i % 8 == 7) System.out.write('\n');
else System.out.write('\t');
}
System.out.write('\n');
}
}
import java.io.*;
public class AsciiChart
{
public static void main(String[] args) {
for (int i = 32; i < 127; i++) {
System.out.write(i);
// break line after every eight characters.
if (i % 8 == 7) System.out.write('\n');
else System.out.write('\t');
}
System.out.write('\n');
}
}
Subscribe to:
Posts (Atom)