为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

自考JAVA语言程序设计(一)课后习题答案和源代码(实验大纲)

2017-09-30 44页 doc 167KB 105阅读

用户头像

is_792768

暂无简介

举报
自考JAVA语言程序设计(一)课后习题答案和源代码(实验大纲)自考JAVA语言程序设计(一)课后习题答案和源代码(实验大纲) 实验大纲 1 字符统计程序 程序运行结果: 统计字符源文件:StaChar.java import javax.swing.*; /** * 1 字符统计程序 * 利用对话框读入字符串 统计输入字符行中数字字符、英文字母个数. * @author 黎明你好 */ public class StaChar { public static void main(String[] args) { String str = JOptionP...
自考JAVA语言程序设计(一)课后习题答案和源代码(实验大纲)
自考JAVA语言程序设计(一)课后习题答案和源代码(实验大纲) 实验大纲 1 字符统计程序 程序运行结果: 统计字符源文件:StaChar.java import javax.swing.*; /** * 1 字符统计程序
* 利用对话框读入字符串 统计输入字符行中数字字符、英文字母个数.
* @author 黎明你好 */ public class StaChar { public static void main(String[] args) { String str = JOptionPane.showInputDialog("请输入字符串:"); char[] c = str.toCharArray(); int numberCount = 0; int letterCount = 0; for (int i = 0; i < c.length; i++) { if (c[i] < '9' && c[i] > '0') numberCount++; else if ((c[i] > 'A' && c[i] < 'Z') || (c[i] > 'a' && c[i] < 'z')) letterCount++; } String result = "输入内容:\n" + str + "\n数字字符:" + numberCount + "个; " + "\n字母:" + letterCount + "个"; JOptionPane.showMessageDialog(null, result, "结果:", JOptionPane.INFORMATION_MESSAGE); } } 2 找质数程序 程序运行结果: 输出质数原文件:PrintPrime.java import javax.swing.JOptionPane; /** * 2 找质数程序,利用对话框读入整数,输出2至这个整数之间的质数.
* @author 黎明你好 */ public class PrintPrime { private int number;// 正整数 private String result = ""; public PrintPrime()// 构造方法 { number = getIntegerNumber("输入整数n", 0);// 要求是>=0的整数 if (number < 0) { return;// 出现错误,程序结束 } else // 如果大于等于2,开始用循环计算结果 { for (int i = 2; i <= number; i++) // 计算素数和 { if (isPrimeNumber(i)) result += i + " "; } } // 显示最后的和 JOptionPane.showMessageDialog(null, number + "之前所有素数为:\n “" + result + "”", "显示结果", JOptionPane.INFORMATION_MESSAGE); } /** * 通过图形界面,得到符合规则的正整数的方法 * @param message - 在弹出的对话框中,显示提示信息 * @param min - 要求此数必须大于等于min * @return - 返回符合规则的整数 */ public int getIntegerNumber(String message, int min) { String str = JOptionPane.showInputDialog(null, message, "提示信 息", JOptionPane.INFORMATION_MESSAGE); int number = -1; try { number = Integer.parseInt(str); // 得到输入的正整数 } catch( Exception e ) { JOptionPane.showMessageDialog(null, "输入非数字字符\n程序结束 ", "错误警告", JOptionPane.ERROR_MESSAGE); return -1; // 输入的不是数字字符,程序结束 } if (number < min) { JOptionPane.showMessageDialog(null, "输入的数不符合规则,不是正 整数\n程序结束", "错误警告", JOptionPane.ERROR_MESSAGE); return -1; // 输入的数不是大于2的正整数时候,程序结束 } else return number; } /** * 判断是否是素数的方法 * @param n - 需要判断的数 * @return - 是素数返回true,否则返回false */ public boolean isPrimeNumber(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; } /** main方法 */ public static void main(String[] args) { new PrintPrime(); } } 3 类的继承定义,包括几何形状类Shape、圆形类Circle.、矩形类Rectangle /** * 几何图形类,抽象类 */ abstract class Shape { public float area() { return 0.0f; } } /** * 圆形类 */ class Circle extends Shape { private float R; public Circle(float r) { R = r; } public float area() { return (float) (Math.PI * R * R); } } /** * 矩形类 */ class Rectangle extends Shape { private float w, h; public Rectangle(float w, float h) { this.w = w; this.h = h; } public float area() { return w * h; } } public class Work11_3 { public static void main(String args[]) { Circle c; Rectangle r; c = new Circle(2.0f); r = new Rectangle(3.0f, 5.0f); System.out.println("圆面积" + returnArea(c)); System.out.println("长方形面积" + returnArea(r)); } static float returnArea(Shape s) { return s.area(); } } 4 数组排序程序 源文件:Work11_4.java import javax.swing.*; import java.util.*; /** * 4 数组排序程序.
* 输入整数序列,对输入的整数进行排序,输出结果.
* @author 黎明你好 */ public class Work11_4 { public static final int RISE = 0; public static final int LOWER = 1; public static void main(String[] args) { String str = JOptionPane.showInputDialog("请输入字符串:"); StringTokenizer token = new StringTokenizer(str, ",.;: "); int mode = Work11_4.RISE;// 排列模式,默认为升序排列 int count = token.countTokens();// 输入的整数的个数 int array[] = new int[count]; int index = 0; while (token.hasMoreTokens()) { try { array[index] = Integer.parseInt(token.nextToken()); index++; } catch( Exception e ) { JOptionPane.showMessageDialog(null, "输入非法字符", "错误警 告", JOptionPane.ERROR_MESSAGE); return;// 输入非法字符时候,直接结束程序 } } sort(array, mode);// 按mode模式,进行排序 String result = new String(); String modeString = new String(); if (mode == Work11_4.RISE) modeString = "升序排列结果为:"; if (mode == Work11_4.LOWER) modeString = "降序排列结果为:"; for (int i = 0; i < array.length; i++) { result = result + array[i] + ","; } if (result != null) JOptionPane .showMessageDialog(null, result, modeString, JOptionPane.INFORMATION_MESSAGE); } /** * 给数组排序的方法 * @param array - 需要排序的数组 * @param mode - 排序的模式,可以为RISE,LOWER */ public static void sort(int array[], int mode) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length - 1; j++) { if (mode == Work11_4.RISE && array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } if (mode == Work11_4.LOWER && array[j] < array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } } 5 字符串处理程序,括号匹配 程序运行结果: 括号匹配检测源文件:CheckBrackets.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import unit9.MyFileFilter; import java.io.*; /** * 5 字符串处理程序.
* 输入程序的源程序代码行,找出可能存在圆括号,花括号不匹配的错误.
* @author 黎明你好 */ public class CheckBrackets extends JFrame implements ActionListener, ItemListener { private static final long serialVersionUID = 1L; private JFileChooser fileChooser; private JButton openFileButton; private JComboBox comboBox; private JTextField showRowStringField; private JTextField showMessageField; private JTextArea textArea; private JPanel northPanel, control_panel; private String rowString[]; private File file = null; public CheckBrackets() { super("检测圆、花括号匹配程序"); fileChooser = new JFileChooser(System.getProperty("user.dir")); openFileButton = new JButton("打开文件"); showRowStringField = new JTextField(); showMessageField = new JTextField(20); textArea = new JTextArea(); comboBox = new JComboBox(); northPanel = new JPanel(); control_panel = new JPanel(); rowString = new String[1000]; fileChooser.addChoosableFileFilter(new MyFileFilter("txt"));//文件筛选 textArea.setLineWrap(true); showRowStringField.setEditable(false); showRowStringField.setBackground(Color.WHITE); showMessageField.setEditable(false); showMessageField.setBackground(Color.WHITE); openFileButton.addActionListener(this); comboBox.addItemListener(this); comboBox.addItem("请选择"); control_panel.add(openFileButton); control_panel.add(new JLabel(" 选择代码行:")); control_panel.add(comboBox); control_panel.add(new JLabel("检测结果:")); control_panel.add(showMessageField); northPanel.setLayout(new GridLayout(2, 1, 10, 10)); northPanel.add(control_panel); northPanel.add(showRowStringField); this.add(northPanel, BorderLayout.NORTH); this.add(new JScrollPane(textArea), BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(50, 50, 550, 500); this.setVisible(true); this.validate(); } public void actionPerformed(ActionEvent e) { showMessageField.setText(""); int message = fileChooser.showOpenDialog(this); if (message == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); comboBox.removeAllItems(); comboBox.addItem("请选择"); readFile(file); } } public void itemStateChanged(ItemEvent e) { int index = comboBox.getSelectedIndex(); if (index >= 1) { showRowStringField.setText(rowString[index - 1]); char c[] = rowString[index - 1].toCharArray(); int count1 = 0; int count2 = 0; for (int i = 0; i < c.length; i++) { if (c[i] == '{') count1++; if (c[i] == '}') count1--; if (c[i] == '(') count2++; if (c[i] == ')') count2--; System.out.println("大括号" + count1 + ",小括号:" + count2); } if (count1 != 0) showMessageField.setText("第" + index + "行,大括号'{}'匹配错误"); else if (count2 != 0) showMessageField.setText("第" + index + "行,小括号'()'匹配错误"); else if (count1 != 0 && count2 != 0) showMessageField.setText("第" + index + "行,大、小括号都匹配错误"); else if (count1 == 0 && count2 == 0) showMessageField.setText("括号匹配正确"); } } public void readFile(File f) { if (f != null) try { FileReader file = new FileReader(f); BufferedReader in = new BufferedReader(file); String s = new String(); int i = 0; textArea.setText(""); while ((s = in.readLine()) != null) { textArea.append((i + 1) + ":" + s + "\n"); rowString[i] = s; comboBox.addItem(i + 1); i++; } } catch( Exception e ) { System.out.println("" + e.toString()); } } public static void main(String[] args) { new CheckBrackets(); } } 6 计算器程序。 程序运行结果: 简易计算器程序原文件: Calculator.java import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 计算器程序.
* 三个文本框,加、减、乘、除按钮 在前两个文本框分别输入两个运算数 点击按钮后,在第 三个文本框中显示计算结果.
* @author 黎明你好 */ public class Calculator extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JTextField oneField, twoField, resultField; private JButton addButton, subtractButton, multiplyButton, divideButton, cleanButton; private JPanel panel1, panel2, panel3; public Calculator() { super("简易计算器"); oneField = new JTextField(10); twoField = new JTextField(10); resultField = new JTextField(20); addButton = new JButton("+"); subtractButton = new JButton(","); multiplyButton = new JButton("*"); divideButton = new JButton("/"); cleanButton = new JButton("CE"); panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); oneField.setHorizontalAlignment(JTextField.RIGHT); twoField.setHorizontalAlignment(JTextField.RIGHT); resultField.setHorizontalAlignment(JTextField.RIGHT); resultField.setEditable(false); resultField.setBackground(Color.WHITE); resultField.setForeground(Color.RED); panel3.setLayout(new GridLayout(1, 4, 5, 5)); panel3.add(addButton); panel3.add(subtractButton); panel3.add(multiplyButton); panel3.add(divideButton); panel3.add(cleanButton); addButton.addActionListener(this); subtractButton.addActionListener(this); multiplyButton.addActionListener(this); divideButton.addActionListener(this); cleanButton.addActionListener(this); panel1.add(new JLabel("输入x:")); panel1.add(oneField); panel2.add(new JLabel("输入y:")); panel2.add(twoField); this.setLayout(new FlowLayout()); this.add(panel1); this.add(panel2); this.add(panel3); this.add(resultField); this.setBounds(200, 100, 300, 200); this.setVisible(true); this.validate(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { double x = 0; double y = 0; try { x = Double.parseDouble(oneField.getText()); y = Double.parseDouble(twoField.getText()); } catch( NumberFormatException e1 ) { resultField.setText("请输入数字字符"); } if (e.getSource() == addButton) { resultField.setText("X + Y = " + (x + y)); } else if (e.getSource() == subtractButton) { resultField.setText("X - Y = " + (x - y)); } else if (e.getSource() == multiplyButton) { resultField.setText("X ?Á Y = " + (x * y)); } else if (e.getSource() == divideButton) { if (y == 0) resultField.setText("除数不能为0"); else resultField.setText("X ? Y = " + (x / y)); } if (e.getSource() == cleanButton) { oneField.setText(""); twoField.setText(""); resultField.setText(""); } } public static void main(String[] args) { new Calculator(); } } 7 选择框应用程序。 程序运行结果: 选择框程序源文件: Work11_7.java import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 7 选择框应用程序.
* 使用选择框选择商品,在文本框显示商品的单价、产地等信息.
* @author 黎明你好 */ public class Work11_7 extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel panel1, panel2; private Goods goods1, goods2, goods3, goods4; private JTextField showNameText; // 显示商品名字 private JTextField showCostText; // 显示单价 private JTextField showPlaceText;// 显示产地 private JTextField showWeightText; // 显示重量 private ButtonGroup group; public Work11_7() { super("选择框应用程序"); panel1 = new JPanel(); panel2 = new JPanel(); group = new ButtonGroup(); goods1 = new Goods("高露洁牙膏", 10.45, "广州", 850); goods2 = new Goods("飘柔洗发露", 16.90, "天津", 1530.5); goods3 = new Goods("老干妈肉酱", 9.80, "贵阳", 210); goods4 = new Goods("可比克薯片", 8.50, "吉林", 45); showNameText = new JTextField(10); showCostText = new JTextField(10); showPlaceText = new JTextField(10); showWeightText = new JTextField(10); addGoods(goods1); addGoods(goods2); addGoods(goods3); addGoods(goods4); panel2.setLayout(new GridLayout(4, 2)); panel2.add(new JLabel("商品名称:")); panel2.add(showNameText); panel2.add(new JLabel("商品单价:")); panel2.add(showCostText); panel2.add(new JLabel("商品产地:")); panel2.add(showPlaceText); panel2.add(new JLabel("商品重量:")); panel2.add(showWeightText); this.setLayout(new FlowLayout()); this.add(panel1); this.add(panel2); this.setBounds(200, 100, 400, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void addGoods(Goods goods) { JCheckBox box = new JCheckBox(goods.getName()); group.add(box); box.addActionListener(this); panel1.add(box); } public void actionPerformed(ActionEvent e) { String name = e.getActionCommand(); if (name.equals(goods1.getName())) setGoodsText(goods1); if (name.equals(goods2.getName())) setGoodsText(goods2); if (name.equals(goods3.getName())) setGoodsText(goods3); if (name.equals(goods4.getName())) setGoodsText(goods4); } public void setGoodsText(Goods goods) { showNameText.setText("" + goods.getName()); showPlaceText.setText("" + goods.getPlace()); showCostText.setText("" + goods.getCost() + " 元"); showWeightText.setText("" + goods.getWeight() + " 克"); } public static void main(String args[]) { new Work11_7(); } } 用到的商品类源文件:Goods.java /** * 商品类 */ class Goods { private String name;// 商品名称 private double cost;// 商品单价,单位元 private String place;// 商品产地 private double weight;// 商品重量,单位克 public Goods(String name, double cost, String place, double weight) { this.name = name; this.cost = cost; this.place = place; this.weight = weight; } public String getName() { return name; } public String getPlace() { return place; } public double getCost() { return cost; } public double getWeight() { return weight; } } 8 菜单应用程序。 程序运行结果: 菜单练习程序源文件:MenuFrame.java import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 菜单应用程序.
* 一个菜单,一个菜单条含三个下拉式菜单,每个下拉式菜单又有2到3个菜单项.
* 当选择某个菜单项时,弹出一个对话框显示菜单项的选择信息.
* @author 黎明你好 */ public class MenuFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JMenuBar menubar; private JMenu file_menu, edit_menu, look_menu, arrangeIcons_menu, tool_menu; private JMenuItem new_item, open_item; private JMenuItem copy_item, paste_item; private JMenuItem refresh_item, byGroup_item, auto_item; private JTextArea textArea; public MenuFrame() { super("菜单应用程序"); textArea = new JTextArea(); menubar = new JMenuBar(); file_menu = new JMenu("文件"); edit_menu = new JMenu("编辑"); look_menu = new JMenu("查看"); tool_menu = new JMenu("工具栏"); arrangeIcons_menu = new JMenu("排列图标"); new_item = new JMenuItem("新建"); open_item = new JMenuItem("打开"); copy_item = new JMenuItem("复制"); paste_item = new JMenuItem("粘贴"); refresh_item = new JMenuItem("刷新"); byGroup_item = new JMenuItem("按组排列"); auto_item = new JMenuItem("自动排列"); menubar.add(file_menu); menubar.add(edit_menu); menubar.add(look_menu); file_menu.add(new_item); file_menu.add(open_item); edit_menu.add(copy_item); edit_menu.add(paste_item); look_menu.add(refresh_item); look_menu.add(tool_menu); look_menu.add(arrangeIcons_menu); arrangeIcons_menu.add(byGroup_item); arrangeIcons_menu.add(auto_item); new_item.addActionListener(this); open_item.addActionListener(this); copy_item.addActionListener(this); paste_item.addActionListener(this); refresh_item.addActionListener(this); byGroup_item.addActionListener(this); auto_item.addActionListener(this); this.setJMenuBar(menubar); this.add(textArea, BorderLayout.CENTER); this.setBounds(50, 50, 250, 200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String name = e.getActionCommand(); JOptionPane.showMessageDialog(this, name + " 背选中", "提示信息", JOptionPane.INFORMATION_MESSAGE); } public static void main(String[] args) { new MenuFrame(); } } 9 多线程应用程序。 程序运行结果: 多线程程序源文件ThreadFrame.java import java.awt.BorderLayout; import java.awt.event.*; import javax.swing.*; ** * 多线程应用程序.
* 爸爸和妈妈不断往盘子中放桃子.
* 三个儿子不断吃盘子汇总的桃子,吃的速度不一样.
* 不能同时操作盘子,盘子最多可以放5个桃子.
* @author 黎明你好 */ public class ThreadFrame extends JFrame implements Runnable, ActionListener { private static final long serialVersionUID = 1L; private JPanel panel; private JButton button; private JTextArea textArea; private Thread fatherThread, motherThread, childOneThread, childTwoThread, childThreeThread; private int peachCount = 0; // 盘子桃子数量 public static final int EAT = 0;//动作,吃桃子 public static final int PRODUCE = 1;//动作,放桃子 public static final int COUNTDISH = 5;// 盘子可以放桃子总数 public static final int COUNT = 50; // 总的执行次数,防止程序一直执行下 去 private int number = 0; public ThreadFrame() { super("多线程应用程序"); fatherThread = new Thread(this); motherThread = new Thread(this); childOneThread = new Thread(this); childTwoThread = new Thread(this); childThreeThread = new Thread(this); panel = new JPanel(); button = new JButton("开始程序"); textArea = new JTextArea(); button.addActionListener(this); panel.add(button); this.add(panel, BorderLayout.NORTH); this.add(new JScrollPane(textArea), BorderLayout.CENTER); this.setBounds(10, 10, 500, 700); this.setVisible(true); this.validate(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { fatherThread.start(); motherThread.start(); childOneThread.start(); childTwoThread.start(); childThreeThread.start(); } public void run() { if (Thread.currentThread() == fatherThread) { while (number < COUNT) { dish(PRODUCE, " 爸爸 - "); try { Thread.sleep(100); } catch( InterruptedException e ) { e.printStackTrace(); } number++; } } if (Thread.currentThread() == motherThread) { while (number < COUNT) { dish(PRODUCE, " 妈妈 - "); try { Thread.sleep(200); } catch( InterruptedException e ) { e.printStackTrace(); } number++; } } if (Thread.currentThread() == childOneThread) { while (number < COUNT) { dish(EAT, " 老大 - "); try { Thread.sleep(1000); } catch( InterruptedException e ) { e.printStackTrace(); } number++; } } if (Thread.currentThread() == childTwoThread) { while (number < COUNT) { dish(EAT, " 老二 - "); try { Thread.sleep(500); } catch( InterruptedException e ) { e.printStackTrace(); } number++; } } if (Thread.currentThread() == childThreeThread) { while (number < COUNT) { dish(EAT, " 老三 - "); try { Thread.sleep(300); } catch( InterruptedException e ) { e.printStackTrace(); } number++; } } } /** * 对盘子操作的方法 * @param action - 动作,分为吃EAT,和放PRODUCE * @param name - 动作的产生者 */ public synchronized void dish(int action, String name) { if (action == EAT) { while (true) { if (peachCount >= 1) break; else { try { textArea.append("eat peach wait " + name + "吃桃子 等待\n"); wait(); } catch( InterruptedException e ) { e.printStackTrace(); } } } peachCount--; textArea.append(name + "吃掉一个桃子\n"); textArea.append("----------------盘子中桃子数量为:" + peachCount + "个\n"); } if (action == PRODUCE) { while (true) { if (peachCount < COUNTDISH) break; else { try { textArea.append("produce peach wait" + name + "放 桃子等待\n"); wait(); } catch( InterruptedException e ) { e.printStackTrace(); } } } peachCount++; textArea.append(name + "放上一个桃子\n"); textArea.append("----------------盘子中桃子数量为:" + peachCount + "个\n"); } notify(); } public static void main(String[] args) { new ThreadFrame(); } } 10 数据文件应用程序。 原文件:OpenAndSaveFile.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; /** * 保存和打开文件 * @author 黎明你好 */ public class OpenAndSaveFile extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JFileChooser fileChooser;// 文件选择对话框 private JPanel northPanel;// 布局用的panel private JButton openFileButton, saveFileButton;// 打开和保存文件按钮 private JLabel label;// 显示文件的绝对路径 private JTextArea textArea;// 显示文件内容的文本区 private File file = null;// 文件对象 public OpenAndSaveFile() { super("保存和打开文件"); label = new JLabel(); fileChooser = new JFileChooser(); northPanel = new JPanel(); openFileButton = new JButton("打开文件"); saveFileButton = new JButton("保存文件"); textArea = new JTextArea(); fileChooser.addChoosableFileFilter(new MyFileFilter("txt")); openFileButton.addActionListener(this); saveFileButton.addActionListener(this); northPanel.add(openFileButton); northPanel.add(saveFileButton); this.add(northPanel, BorderLayout.NORTH); this.add(new JScrollPane(textArea), BorderLayout.CENTER); this.add(label, BorderLayout.SOUTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(50, 50, 500, 500); this.setVisible(true); this.validate(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == openFileButton) { int message = fileChooser.showOpenDialog(this); if (message == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); if (file != null) { label.setText(file.getAbsolutePath()); showFile(file); } } else { label.setText("没有文件被选中"); } } if (e.getSource() == saveFileButton) { int message = fileChooser.showSaveDialog(this); if (message == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); label.setText("保存到:" + file.getAbsolutePath()); saveFile(file); } else { label.setText("没有文件被选中"); } } } /** * 打开指定文件,显示在文本区上 * @param f - 文件对象 */ public void showFile(File f) { try { FileReader file = new FileReader(f); BufferedReader in = new BufferedReader(file); String s = new String(); textArea.setText(""); while ((s = in.readLine()) != null) { textArea.append(s + "\n"); } in.close(); } catch( Exception e ) { label.setText("读文件发生错误"); } } /** * 把文本区上的内容保存到制定文件 * @param f - 要保存到的文件对象 */ public void saveFile(File f) { try { FileWriter file = new FileWriter(f); BufferedWriter out = new BufferedWriter(file); out.write(textArea.getText(), 0, textArea.getText().length()); out.close(); } catch( Exception e ) { label.setText("写文件发生错误"); } } public static void main(String[] args) { new OpenAndSaveFile(); } }
/
本文档为【自考JAVA语言程序设计(一)课后习题答案和源代码(实验大纲)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索