Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sascha Tayefeh http://www.tayefeh.de

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Trivial AWT-only Event Handling

------------------------------------------------------------
1. Main Class
------------------------------------------------------------

class Main {

        public static void main(final String[] args) {
                MainFrameCommand cmd = new MainFrameCommand();
                MainFrameGUI gui = new MainFrameGUI(cmd);
        }

}

------------------------------------------------------------
2. MainFrameCommand Class
------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;

class MainFrameCommand
implements KeyListener, MouseMotionListener, WindowListener {

        /* Key Listener */
        public void keyPressed(KeyEvent event) {}
        public void keyReleased(KeyEvent event) {}
        public void keyTyped(KeyEvent event) {}

        /* MouseMotion Listener */
        public void mouseMoved(MouseEvent event) {}
        public void mouseDragged(MouseEvent event) {}

        /* WindowListener */
        public void windowClosed(WindowEvent event) {}
        public void windowOpened(WindowEvent event) {}
        public void windowClosing(WindowEvent event) {}
        public void windowActivated(WindowEvent event) {}
        public void windowDeactivated(WindowEvent event) {}
        public void windowIconified(WindowEvent event) {}
        public void windowDeiconified(WindowEvent event) {}

}


------------------------------------------------------------
3. MainFrameGUI Class
------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;

class MainFrameGUI extends Frame {
                public MainFrameGUI(MainFrameCommand cmd) {
                super("Window");
                setSize(300, 300);
                setVisible(true);


                addKeyListener(cmd);
                addWindowListener(cmd);
                addMouseMotionListener(cmd);
        }

        public void paint(Graphics g) {}
} 

Read File Linewise with Java


Here's the class

import java.io.*;

public class MyIO {
	void printFile(String fileName) {
		BufferedReader rd;
		String line;

		try {
			rd = new BufferedReader(new FileReader(fileName));
						
			while ((line =  rd.readLine()  ) != null) {
				System.out.println(line);
			}
			rd.close();
		} catch (final IOException e) {
			System.out.println("Error reading file");
		}

	}

}



All along with an example usage:

	MyIO myIO = new MyIO();
	myIO.printFile("filename.txt");

Using java.io.File object

Using java.io.File object

import java.io.*;

class Main {
	static String filename = new String("/home/sascha/temp/changes.xml");	
	
	public static void main(String[] args) {
		File fileObject;
		System.out.println("Reading File: " + filename);

		fileObject = new File(filename);
		
		System.out.println("Filename: " + fileObject.getName());
		System.out.println("Length: " + (fileObject.length()) + "bytes");
		System.out.println("Last modified timesptamp: " + fileObject.lastModified());
	}

}

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS