import java.io.File;
public class FileEx {
public static void main(String[] args) {
File file = new File("c:\\windows\\system.ini");
System.out.println(file.getPath());
System.out.println(file.getParent());
System.out.println(file.getName());
if (file.isFile()) {
System.out.println("파일입니다.");
} else {
System.out.println("디렉토리입니다.");
}
File file02 = new File("c:\\Temp\\java");
if (!file02.exists()) {
file02.mkdir();
}
File file03 = new File("c:\\Temp");
File subFiles[] = file03.listFiles();
for (int i = 0; i < subFiles.length; i++) {
File f = subFiles[i];
System.out.println(f.getName());
System.out.println(f.isFile() ? "파일" : "폴더");
}
}
}
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
/**
* FileInputSteamEx
*/
public class FileInputSteamEx {
public static void main(String[] args) {
byte b[] = new byte[6];
try {
FileInputStream fileInput = new FileInputStream("c:\\Temp\\test.out");
int n = 0;
int c = 0;
while ((c = fileInput.read()) != -1) {
b[n] = (byte) c;
n++;
}
fileInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class FileOutputSteamEx {
public static void main(String[] args) {
byte b[] = { 7, 51, 3, 4, 24, -1 };
try {
FileOutputStream fout = new FileOutputStream("c:\\Temp\\test.out");
for (int i = 0; i < b.length; i++) {
fout.write(b[i]);
}
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//import java.io.FileNotFoundException;
import java.io.FileReader;
public class FileReaderEx {
public static void main(String[] args) {
FileReader fio = null;
try {
fio = new FileReader("c:\\windows\\system.ini");
int c;
while ((c = fio.read()) != -1) {
System.out.print((char) c);
}
fio.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.InputStreamReader;
//import java.io.UnsupportedEncodingException;
public class FileReadHangul {
public static void main(String[] args) {
InputStreamReader in = null;
FileInputStream fin = null;
try {
fin = new FileInputStream("c:\\Temp\\hangul.txt");
in = new InputStreamReader(fin, "utf-8");
System.out.println(in.getEncoding());
int c;
while ((c = in.read()) != -1) {
System.out.print((char) c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileWriterEx {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("c:\\Temp\\test.txt");
while (true) {
String line = scanner.nextLine();
if (line.length() == 0) {
break;
}
fileWriter.write(line, 0, line.length());
fileWriter.write("\r\n", 0, 2);
}
fileWriter.close();
scanner.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedOutputStream;
//import java.io.FileNotFoundException;
import java.io.FileReader;
public class BufferedIOEx {
public static void main(String[] args) {
FileReader fin = null;
int c;
try {
fin = new FileReader("c:\\Temp\\test02.txt");
// buffer는 정해진 사이즈를 넘어서야지만 전송 시작 강제로 보내고 싶을때는 flush();
BufferedOutputStream out = new BufferedOutputStream(System.out, 20);
while ((c = fin.read()) != -1) {
out.write(c);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.Vector;
class WordSearch {
private Vector<String> wordList = new Vector<String>();
private void wordRead() {
try {
File file = new File("c:\\Temp\\words.txt");
Scanner readWord = new Scanner(new FileReader(file));
while (readWord.hasNext()) {
String line = readWord.nextLine();
wordList.add(line);
}
System.out.println(
"프로젝트 폴더 밑의 " + file.getName() + " 파일을 읽었습니다...."
);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void wordSearching() {
Scanner scanner = new Scanner(System.in);
int cnt = 0;
while (true) {
System.out.print("단어>> ");
String Input = scanner.nextLine();
if (Input.equals("그만")) {
System.out.println("종료합니다....");
break;
}
for (int i = 0; i < wordList.size(); i++) {
if (wordList.get(i).startsWith(Input)) {
System.out.println(wordList.get(i));
cnt += 1;
}
}
if (cnt == 0) {
System.out.println("발견할 수 없음");
}
}
scanner.close();
}
public void run() {
wordRead();
wordSearching();
}
}
public class Practice11 {
public static void main(String[] args) {
WordSearch wordSearch = new WordSearch();
wordSearch.run();
}
}
package Practice;
import java.awt.*;
import javax.swing.*;
public class Practice07 extends JFrame {
public Practice07() {
setTitle("Calc");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setVisible(true);
Container contentPane = getContentPane();
JPanel northJ = new JPanel();
northJ.setBackground(Color.gray);
northJ.setSize(500, 50);
northJ.setLayout(new FlowLayout());
northJ.add(new JLabel("수식 입력"));
northJ.add(new JTextField(16));
JPanel centerJ = new JPanel();
centerJ.setLayout(new GridLayout(4, 4, 5, 5));
for (int i = 0; i < 10; i++) {
JButton btn = new JButton(" " + i);
centerJ.add(btn);
}
String mark[] = { "CE", "계산", "+", "-", "x", "/" };
for (int i = 0; i < mark.length; i++) {
if (i >= 0 || i < 2) {
JButton btn = new JButton(mark[i]);
centerJ.add(btn);
} else if (i >= 2 || i < mark.length) {
JButton btn = new JButton(mark[i]);
centerJ.add(btn).setBackground(Color.GREEN);
}
}
JPanel southJ = new JPanel();
southJ.setBackground(Color.yellow);
southJ.setSize(500, 50);
southJ.setLayout(new FlowLayout());
southJ.add(new JLabel("계산 결과"));
southJ.add(new JTextField(16));
contentPane.add(northJ, BorderLayout.NORTH);
contentPane.add(centerJ, BorderLayout.CENTER);
contentPane.add(southJ, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new Practice07();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyActionListener implements ActionListener {
public void gugudanOut(int dan) {
for (int j = 1; j <= 9; j++) {
int result = dan * j;
System.out.println(dan + " x " + j + " = " + result);
}
System.out.println();
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource(); // 선택된 구구단 버튼
if (button.getText().equals("1")) {
button.setText("One");
gugudanOut(1);
} else if (button.getText().equals("2")) {
button.setText("Two");
gugudanOut(2);
} else if (button.getText().equals("3")) {
button.setText("Three");
gugudanOut(3);
} else if (button.getText().equals("4")) {
button.setText("Four");
gugudanOut(4);
} else if (button.getText().equals("5")) {
button.setText("Five");
gugudanOut(5);
} else if (button.getText().equals("6")) {
button.setText("Six");
gugudanOut(6);
} else if (button.getText().equals("7")) {
button.setText("Seven");
gugudanOut(7);
} else if (button.getText().equals("8")) {
gugudanOut(8);
button.setText("Eight");
} else if (button.getText().equals("9")) {
gugudanOut(9);
button.setText("Nine");
} else {
System.out.println();
}
}
}
public class IndependentClassListener extends JFrame {
public IndependentClassListener() {
this.setTitle("Action Event");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3, 3, 10, 10));
JButton btn[] = new JButton[9];
for (int i = 0; i < 9; i++) {
btn[i] = new JButton(Integer.toString(i + 1));
contentPane.add(btn[i]);
}
for (int i = 0; i < btn.length; i++) {
btn[i].addActionListener(new MyActionListener());
}
for (int i = 0; i < btn.length; i++) {
contentPane.add(btn[i]);
}
this.setSize(400, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new IndependentClassListener();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnonymousClassListener extends JFrame {
public AnonymousClassListener() {
this.setTitle("Action Event");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
JButton btn = new JButton("Action");
btn.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
if (btn.getText().equals("Action")) {
btn.setText("액션!");
} else {
btn.setText("Action");
}
setTitle("바껴라");
}
}
);
contentPane.add(btn);
this.setSize(400, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new AnonymousClassListener();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseEventEx extends JFrame {
private JLabel la = new JLabel("Hello");
class MyMouseListener implements MouseListener {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
la.setLocation(x, y);
}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public MouseEventEx() {
setTitle("Moust Event 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.addMouseListener(new MyMouseListener());
c.setLayout(null);
la.setSize(50, 20);
la.setLocation(30, 30);
c.add(la);
setSize(250, 250);
setVisible(true);
}
public static void main(String[] args) {
new MouseEventEx();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseAdapterEx extends JFrame {
class myMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
System.out.println("Connected..");
}
}
private JLabel label = new JLabel("hello");
public MouseAdapterEx() {
this.setTitle("Mouse Adapter");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.addMouseListener(new myMouseAdapter());
contentPane.setLayout(null);
label.setLocation(100, 100);
label.setSize(50, 20);
contentPane.add(label);
this.setSize(300, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new MouseAdapterEx();
}
}
package practice;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MovingHello extends JFrame {
private JLabel label = new JLabel("Hello");
private int moving = 10;
class MyMouseAdapter extends MouseAdapter {
// 마우스 찍은 위치로 Hello 옮기기
public void mousePressed(MouseEvent e) {
//super.mousePressed(e);
int x = e.getX();
int y = e.getY();
label.setLocation(x, y);
}
}
class MyKeyListener extends KeyAdapter {
// 상하좌우 키로 Hello 옮기기
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
label.setLocation(label.getX(), label.getY() - moving);
} else if (keyCode == KeyEvent.VK_DOWN) {
label.setLocation(label.getX(), label.getY() + moving);
} else if (keyCode == KeyEvent.VK_LEFT) {
label.setLocation(label.getX() - moving, label.getY());
} else if (keyCode == KeyEvent.VK_RIGHT) {
label.setLocation(label.getX() + moving, label.getY());
}
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
}
public MovingHello() {
this.setTitle("KeyListener");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());
// 마우스 찍은 위치로 Hello 옮기기
contentPane.addMouseListener(new MyMouseAdapter());
// 상하좌우 키로 Hello 옮기기
contentPane.addKeyListener(new MyKeyListener());
contentPane.add(label);
this.setSize(300, 300);
this.setVisible(true);
contentPane.setFocusable(true); // 이게 있어야 키보드 입력 이벤트 가능
contentPane.requestFocus(); // 이것도 마찬가지
}
public static void main(String[] args) {
new MovingHello();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseDoubleClickEx extends JFrame {
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
int clickCounts = e.getClickCount();
Container c = (Container) e.getSource();
if (clickCounts > 1 && c.getBackground() == Color.GREEN) {
c.setBackground(Color.WHITE);
} else if (clickCounts > 1) {
c.setBackground(Color.GREEN);
}
}
}
public MouseDoubleClickEx() {
this.setTitle("double click and back color will change");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.add(
new JLabel("더블클릭:초록 / 다시 더블클릭:디폴트"),
BorderLayout.CENTER
);
contentPane.addMouseListener(new MyMouseListener());
this.setSize(300, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new MouseDoubleClickEx();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseListenerAllEx extends JFrame {
JLabel label = new JLabel("No Mouse Event");
// 인터페이스는 이중상속(구현) 가능
class MyMouseListener implements MouseListener, MouseMotionListener {
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
label.setText("Mouse Pressed / " + e.getX() + " / " + e.getY());
}
public void mouseReleased(MouseEvent e) {
label.setText("Mouse Released / " + e.getX() + " / " + e.getY());
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
label.setText("Mouse Dragged / " + e.getX() + " / " + e.getY());
}
public void mouseMoved(MouseEvent e) {
label.setText("Mouse Moved / " + e.getX() + " / " + e.getY());
}
}
public MouseListenerAllEx() {
this.setTitle("All Mouse Event");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
contentPane.addMouseListener(new MyMouseListener());
contentPane.addMouseMotionListener(new MyMouseListener());
contentPane.setLayout(new BorderLayout());
contentPane.add(label, BorderLayout.CENTER);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
new MouseListenerAllEx();
}
}