0
点赞
收藏
分享

微信扫一扫

Java二级操作题第25套

基本操作

在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

程序的功能是对两个内容相同的串采用两种方式进行比较,会得到不同的结果。该程序运行时的输出结果如下:
false
true

// 阅读下列代码:
public class Java_1
{
//*********Found**********
public static void ____________________(_____________________ []args)
{
String s1=new String("你正在考试");
String s2=new String("你正在考试");
System.out.println(s1==s2);
//*********Found**********
System.out.println(__________________(s2));
}
}

本题考查的是对字符串的比较。
执行程序需要一个入口,这个入口是main方法,所以第一空填写"main",第二空填写"String"。
字符串的比较不能直接用双等于,而是用equals方法,所以第三空填写"s1.equals"。

具体程序如下:

// 阅读下列代码:
public class Java_1
{
//*********Found**********
public static void main(String []args)
{
String s1=new String("你正在考试");
String s2=new String("你正在考试");
System.out.println(s1==s2);
//*********Found**********
System.out.println(s1.equals(s2));
}
}

简单应用

在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

程序的功能是:以两个文件名做为命令行参数,如果两个文件都存在,则将第二个文件的内容追加到第一个文件中,并将第二个文件从系统中删除。
程序运行时,将“a.txtb.txt”作为命令行参数,会显示:
has done!
SUCCESS!

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Java_2
{
public static void main(String args[])
{
if(args.length<2)
{
System.out.println("ERROR: need parameters.");
System.out.println("\n -usage: java <classname> <file1> <file2>");
System.exit(0);
}
File f1=new File(args[0]);
//*********Found**********
File f2=new File(_____________________);
try
{
//*********Found**********
FileReader fr=new FileReader(______________________);
FileWriter fw=new FileWriter(f1,true);
int b;
//*********Found**********
while(( b=fr.read() ) != -1 ) fw.write(_____________________);
fr.close();
fw.close();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("has done!");
//*********Found**********
if(f2.delete()) System.out.print("___________");
}
}

本题考查的是对主函数参数的理解。

由题意可知,args数组中是两个文件名,f1已经用了args[0]表示,那么第二个文件自然是args[1],所以第一空填写"args[1]"。

由题意得知,需要将第二个文件加入到第一个文件并删除第二个文件,FileReader是读取文件内容的类,所以第二空填写"f2"。

b=fr.read()中的b表示的是第二个文件的位置,所以当需要写入第一个文件时,则将b传入write,所以第三空填写"b"。

最后删除时要求输出"SUCCESS!",所以第四空填写"SUCCESS!"。

Java二级操作题第25套_java

具体程序如下:

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Java_2
{
public static void main(String args[])
{
if(args.length<2)
{
System.out.println("ERROR: need parameters.");
System.out.println("\n -usage: java <classname> <file1> <file2>");
System.exit(0);
}
File f1=new File(args[0]);
//*********Found**********
File f2=new File(args[1]);
try
{
//*********Found**********
FileReader fr=new FileReader(f2);
FileWriter fw=new FileWriter(f1,true);
int b;
//*********Found**********
while(( b=fr.read() ) != -1 ) fw.write(b);
fr.close();
fw.close();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("has done!");
//*********Found**********
if(f2.delete()) System.out.print("SUCCESS!");
}
}

综合应用

在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行"//Found"下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

本题的要求是:

1、程序运行界面如下: 

Java二级操作题第25套_文件名_02


2、点击open file按钮,打开文件对话框,选择文件,在文本框中输出所选文件的路径;

3、点击save file按钮,打开文件对话框,在对话框中输入文件名,并在文本框中输出保存文件的路径。

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class Java_3 implements ActionListener
{
private JFrame frame;
private JButton button;
private JButton saveButton;
private JTextArea textArea;
private JFileChooser dia;
private JPanel buttonPanel;

public void initGUI()
{
//*********Found**********
frame=new JFrame("_____________________");

button=new JButton("open file");
button.setActionCommand("open");
//*********Found**********
button.addActionListener(___________);
saveButton=new JButton("save file");
saveButton.setActionCommand("save");
//*********Found**********
saveButton.addActionListener(___________);

textArea=new JTextArea("",10,10);
buttonPanel=new JPanel();
dia=new JFileChooser();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
buttonPanel.add(button);
buttonPanel.add(saveButton);
frame.getContentPane().add(buttonPanel,BorderLayout.NORTH);
frame.getContentPane().add(textArea,BorderLayout.CENTER);
frame.setSize(300,300);
//*********Found**********
frame.setVisible(___________);
}

//*********Found**********
public void actionPerformed(ActionEvent ___________)
{
if(event.getActionCommand().equals("open"))
{
dia.showOpenDialog( frame );
dia.setVisible(true);
File file=dia.getSelectedFile();
String fileName=file.getAbsolutePath();
textArea.append("path of selected file: "+fileName+"\r\n");
}
else if(event.getActionCommand().equals("save"))
{
dia.showSaveDialog(frame);
dia.setVisible(true);
File file=dia.getSelectedFile();
String fileName=file.getAbsolutePath();
textArea.append("path of saved file: "+fileName+"\r\n");
}
}

public static void main(String args[])
{
Java_3 example=new Java_3();
example.initGUI();
}
}

本题考查的是JavaSwing。
JFrame的构造器中带字符串则表示窗口的标题,由于题目可知,窗口标题为file chooser,所以第一空填写"file chooser"。
根据actionPerformed方法可得知,两个按钮的监听事件都写在了Java_3这个类中,所以addActionListener的参数应为this,所以第二空和第三空填写"this"。
setVisible方法表示容器是否显示,所以第四空填写"true"。
event.getActionCommand()方法为ActionEvent类的方法,所以第五空填写"event"。

具体程序如下:

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class Java_3 implements ActionListener
{
private JFrame frame;
private JButton button;
private JButton saveButton;
private JTextArea textArea;
private JFileChooser dia;
private JPanel buttonPanel;

public void initGUI()
{
//*********Found**********
frame=new JFrame("file chooser");

button=new JButton("open file");
button.setActionCommand("open");
//*********Found**********
button.addActionListener(this);
saveButton=new JButton("save file");
saveButton.setActionCommand("save");
//*********Found**********
saveButton.addActionListener(this);

textArea=new JTextArea("",10,10);
buttonPanel=new JPanel();
dia=new JFileChooser();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
buttonPanel.add(button);
buttonPanel.add(saveButton);
frame.getContentPane().add(buttonPanel,BorderLayout.NORTH);
frame.getContentPane().add(textArea,BorderLayout.CENTER);
frame.setSize(300,300);
//*********Found**********
frame.setVisible(true);
}

//*********Found**********
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("open"))
{
dia.showOpenDialog( frame );
dia.setVisible(true);
File file=dia.getSelectedFile();
String fileName=file.getAbsolutePath();
textArea.append("path of selected file: "+fileName+"\r\n");
}
else if(event.getActionCommand().equals("save"))
{
dia.showSaveDialog(frame);
dia.setVisible(true);
File file=dia.getSelectedFile();
String fileName=file.getAbsolutePath();
textArea.append("path of saved file: "+fileName+"\r\n");
}
}

public static void main(String args[])
{
Java_3 example=new Java_3();
example.initGUI();
}
}

箴言:因为这些东西是非常简单的。不要抱怨自己学不会,那是因为你没有足够用心。



举报

相关推荐

0 条评论