السلام عليكم
بما أنني جديدة جداً على الجافا - خبرتي فيها تعد بالأيام - فإنني أريد مساعدتكم في ربط البرنامجيين التاليين ..
البرنامج الأول عبارة عن Editor يفتح ملف جديد أو موجود و يقوم بتخزينه عن طريق menu.
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TrivialEdit extends JFrame {
public static void main(String[] args) {
new TrivialEdit();
}
private JTextArea text;
public TrivialEdit() {
super("A Trivial Editor");
setJMenuBar(makeMenus());
text = new JTextArea(25,50);
text.setBackground(Color.white);
text.setMargin( new Insets(3,5,0,0) );
JScrollPane scroller = new JScrollPane(text);
setContentPane(scroller);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocation(50,50);
show();
}
private JMenuBar makeMenus() {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if (cmd.equals("New"))
doNew();
else if (cmd.equals("Open..."))
doOpen();
else if (cmd.equals("Save..."))
doSave();
else if (cmd.equals("Quit"))
doQuit();
}
};
JMenu fileMenu = new JMenu("File");
JMenuItem newCmd = new JMenuItem("New");
newCmd.setAccelerator( KeyStroke.getKeyStroke("ctrl N") );
newCmd.addActionListener(listener);
fileMenu.add(newCmd);
JMenuItem openCmd = new JMenuItem("Open...");
openCmd.setAccelerator( KeyStroke.getKeyStroke("ctrl O") );
openCmd.addActionListener(listener);
fileMenu.add(openCmd);
JMenuItem saveCmd = new JMenuItem("Save...");
saveCmd.setAccelerator( KeyStroke.getKeyStroke("ctrl S") );
saveCmd.addActionListener(listener);
fileMenu.add(saveCmd);
JMenuItem quitCmd = new JMenuItem("Quit");
quitCmd.setAccelerator( KeyStroke.getKeyStroke("ctrl Q") );
quitCmd.addActionListener(listener);
fileMenu.add(quitCmd);
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
return bar;
}
private void doNew() {
text.setText("");
}
private void doSave() {
File file;
JFileChooser fd;
fd = new JFileChooser(new File("."));
fd.setDialogTitle("Save Text As...");
int action = fd.showSaveDialog(this);
if (action != JFileChooser.APPROVE_OPTION) {
return;
}
file = fd.getSelectedFile();
if (file.exists()) {
action = JOptionPane.showConfirmDialog(this,
"Replace existing file?");
if (action != JOptionPane.YES_OPTION)
return;
}
try {
PrintWriter out = new PrintWriter(new FileWriter(file));
String contents = text.getText();
out.print(contents);
if (out.checkError())
throw new IOException("Error while writing to file.");
out.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(this,
"Sorry, an error has occurred:\n" + e.getMessage());
}
}
private void doOpen() {
File file;
JFileChooser fd;
fd = new JFileChooser(new File("."));
fd.setDialogTitle("Open File...");
int action = fd.showOpenDialog(this);
if (action != JFileChooser.APPROVE_OPTION) {
return;
}
file = fd.getSelectedFile();
try {
TextReader in = new TextReader(new FileReader(file));
String line;
text.setText("");
int lineCt = 0;
while (lineCt < 100 && in.peek() != '\0') {
line = in.getln();
text.append(line + '\n');
lineCt++;
}
if (in.eof() == false)
text.append("\n\n******* Text truncated to 100 lines! *******\n");
in.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this,
"Sorry, some error occurred:\n" + e.getMessage());
}
}
private void doQuit() {
System.exit(0);
}
}و البرنامج الثاني عبارة عن عداد يعد عدد الكلمات و الأسطر و الحروف في ملف و لكنه يعمل من خلال command line
import java.io.*;
/**
* Command line program to count lines, words and characters
* in files or from standard input, similar to the wc
* utility.
* Run like that: java WordCount FILE1 FILE2 ... or
* like that: java WordCount < FILENAME.
* @author Marco Schmidt
*/
public class WordCount {
private static void count(String name, BufferedReader in) throws
IOException {
long numLines = 0;
long numWords = 0;
long numChars = 0;
String line;
do {
line = in.readLine();
if (line != null)
{
numLines++;
numChars += line.length();
numWords += countWords(line);
}
}
while (line != null);
System.out.println(name + "\t" + numLines + "\t" +
numWords + "\t" + numChars);
}
private static void count(String fileName) {
BufferedReader in = null;
try {
FileReader fileReader = new FileReader(fileName);
in = new BufferedReader(fileReader);
count(fileName, in);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
private static void count(String streamName, InputStream input) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(input);
BufferedReader in = new BufferedReader(inputStreamReader);
count(streamName, in);
in.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static long countWords(String line) {
long numWords = 0;
int index = 0;
boolean prevWhitespace = true;
while (index < line.length()) {
char c = line.charAt(index++);
boolean currWhitespace = Character.isWhitespace(c);
if (prevWhitespace && !currWhitespace) {
numWords++;
}
prevWhitespace = currWhitespace;
}
return numWords;
}
public static void main(String[] args) {
if (args.length == 0) {
count("stdin", System.in);
} else {
for (int i = 0; i < args.length; i++) {
count(args);
}
}
}
}ما أريده الآتي :
تحويل الكود الثاني بحيث يتناسب مع الأول فقد وجدت args صعبة علىّ لأفهمها .
مع جزيل شكري مقدماً