import java.awt.*;
import java.awt.event.*;
import java.io.*;
@SuppressWarnings("serial")
public class isuru extends Frame implements ActionListener{
String directory;
TextArea textarea;
public isuru(){
this(null, null);
}
public isuru(String filename){
this(null, filename);
}
public isuru(String directory, String filename){
super();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
}
});
textarea = new TextArea("", 24, 80);
textarea.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
textarea.setEditable(false);
this.add("Center", textarea);
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 5));
this.add(p, "South");
Font font = new Font("SansSerif", Font.BOLD, 14);
Button openfile = new Button("Open File");
Button close = new Button("Close");
openfile.addActionListener(this);
openfile.setActionCommand("open");
openfile.setFont(font);
close.addActionListener(this);
close.setActionCommand("close");
close.setFont(font);
p.add(openfile);
p.add(close);
this.pack();
if(directory == null){
File f;
if((filename != null) && (f = new File(filename)).isAbsolute()){
directory = f.getParent();
filename = f.getName();
}else{
directory = System.getProperty("user.dir");
}
this.directory = directory;
setFile(directory, filename);
}
}
public void setFile(String directory, String filename){
if((filename == null) || filename.length() == 0)
return;
File f;
FileReader in = null;
try{
f = new File(directory, filename);
in = new FileReader(f);
char[] buffer = new char[4096];
int len;
textarea.setText("");
while((len = in.read(buffer)) != -1){
String s = new String(buffer,0,len);
textarea.append(s);
}
this.setTitle("FileViewer: "+filename);
textarea.setCaretPosition(0);
}catch(IOException e){
textarea.setText(e.getClass().getName()+ ": " + e.getMessage( ));
this.setTitle("FileViewer: "+filename+ ": I/O Exception");
}finally{
try{
if(in != null)
in.close();
}catch(IOException e){
}
}
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("open")){
FileDialog f = new FileDialog(this, "Open File");
f.setDirectory(directory);
f.show();
directory = f.getDirectory();
setFile(directory, f.getFile());
f.dispose();
}
else if(cmd.equals("close"))
this.dispose();
}
public static void main(String args[]){
Frame f = new isuru((args.length == 1)?args[0]:null);
f.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.exit(0);
}
});
f.show();
}
}
C++, Java, Python, PHP, Programming Tips, Linux, Bash Shell Scripting, Security And Tech Stuff
Showing posts with label read. Show all posts
Showing posts with label read. Show all posts
Monday, March 21, 2011
Reading and Displaying Text Files
Wednesday, August 11, 2010
How to read a zip file in Java?
This is not a basic tutorial but I just learned it, so I gonna show you how to write simple command line program to read content of zip file.
ZIP archives store one or more files in compressed format. Each ZIP archive has a header with information such as the name of the file and the compression method that was used.
In Java, ZipInputStream class use to read zip archives. To read zip file content one must check each individual entry in the zip file. The getNextEntry() method returns an object of type ZipEntry that describes zip entry. The read method of the ZipInputStream is modified to return -1 at the end of the current entry (instead of just at the end of the ZIP file). You must call closeEntry() method to read the next entry.
Here is my demonstration code I written.
ZIP archives store one or more files in compressed format. Each ZIP archive has a header with information such as the name of the file and the compression method that was used.
In Java, ZipInputStream class use to read zip archives. To read zip file content one must check each individual entry in the zip file. The getNextEntry() method returns an object of type ZipEntry that describes zip entry. The read method of the ZipInputStream is modified to return -1 at the end of the current entry (instead of just at the end of the ZIP file). You must call closeEntry() method to read the next entry.
Here is my demonstration code I written.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.*;
import java.util.zip.ZipInputStream;
public class Main
{
public static void main(String[] args) throws Exception
{
String file = "D:\\kiss.zip";
try
{
FileInputStream fis = new FileInputStream(file);
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = null;
while((ze = zis.getNextEntry()) != null)
{
System.out.println(ze.getName());
zis.closeEntry();
}
zis.close();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
The ZIP input stream throws a ZipException when there is an error in reading a ZIP file. Normally this error occurs when the ZIP file has been corrupted.
Subscribe to:
Posts (Atom)