浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编
版权所有 不得转载 违者必究
try{
BufferedReader br=new BufferedReader(new FileReader(articleName)); word=word.toLowerCase();
while((sentence=br.readLine())!=null){ sentence=sentence.toLowerCase(); int index=sentence.indexOf(word); while(index>=0){ count++;
index=sentence.indexOf(word,index+word.length()); } }
}catch(IOException e){} return count; }
public static void main(String args[]){ String article=\ String word=\
WordCount w=new WordCount();
System.out.println(\单词\在文章\中出现的次数为:\ } }
18、参考代码如下: import java.io.*;
public class N_Digital{
public void initNum(int[] num){ if (num == null) {
throw new NullPointerException(\参数不能为null!\ }
for (int i=0;i num[i]=(int)(Math.random()*40)+10; System.out.print(num[i]+\ } } public void sortNum(int[] obj){ if (obj == null) { throw new NullPointerException(\参数不能为null!\ } int tmp; for (int i = 0 ;i < obj.length ;i++ ){ //切记,每次都要从第一个开始比。最后的不用再比。 for (int j = 0 ;j < obj.length -i -1 ;j++ ) { 第 16 页 共 32 页 浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编 版权所有 不得转载 违者必究 //对邻接的元素进行比较,如果后面的小,就交换 if (obj[j] public void writeToFile(int[]num,String file){ try{ File f=new File(file); if(!f.exists())f.createNewFile(); PrintWriter out = new PrintWriter(new FileWriter(file)); for(int i=0;i System.out.print(num[i]+\ } out.close(); }catch(IOException ioe){ } } public static void main(String []args){ int n=(int)(Math.random()*40)+10; int num[]=new int[n]; N_Digital nd=new N_Digital(); nd.initNum(num); nd.sortNum(num); System.out.println(); nd.writeToFile(num,\ } } 19、参考代码如下: import java.io.*; public class CopyFile { public static void main(String args[]){ FileInputStream from = null; FileOutputStream to = null; try { 第 17 页 共 32 页 浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编 版权所有 不得转载 违者必究 from = new FileInputStream(\ to = new FileOutputStream(\ byte[] buffer = new byte[4096]; int bytes_read; while ( (bytes_read = from.read(buffer)) != -1) { to.write(buffer, 0, bytes_read); } from.close(); to.close(); }catch (IOException e) { e.printStackTrace(); } } } 一个完整的copy文件的参考程序如下: import java.io.*; class CopyFile{ public boolean copy(String fromFileName, String toFileName,boolean override) { File fromFile = new File(fromFileName); File toFile = new File(toFileName); if (!fromFile.exists() || !fromFile.isFile() || !fromFile.canRead()) { return false; } if (toFile.isDirectory()) { toFile = new File(toFile, fromFile.getName()); } if (toFile.exists()) { if (!toFile.canWrite() || override == false) { return false; } } else { String parent = toFile.getParent(); if (parent == null) { parent = System.getProperty(\ } File dir = new File(parent); if (!dir.exists() || dir.isFile() || !dir.canWrite()) { return false; } } FileInputStream from = null; FileOutputStream to = null; 第 18 页 共 32 页 浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编 版权所有 不得转载 违者必究 try { from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytes_read; while ( (bytes_read = from.read(buffer)) != -1) { to.write(buffer, 0, bytes_read); } return true; } catch (IOException e) { return false; } finally { if (from != null) { try { from.close(); } catch (IOException e) { } } if (to != null) { try { to.close(); } catch (IOException e) { } } } } public static void main(String args[]){ CopyFile cf=new CopyFile(); if(cf.copy(\ System.out.println(\文件拷贝成功!\ } else{ System.out.println(\文件未拷贝!\ } } } 20、参考代码如下: import java.io.*; class ReadFileContent{ public static void main(String args[]){ int i=0; 第 19 页 共 32 页 浙江工业大学软件学院《Java程序设计》练习题参考答案——赵小敏 自编 版权所有 不得转载 违者必究 try { BufferedReader br = new BufferedReader(new FileReader(\ String line = br.readLine(); PrintWriter out = new PrintWriter(new FileWriter(\ while(line!=null){ i++; out.println(i+\ line = br.readLine(); } br.close(); out.close(); }catch (Exception e) { e.printStackTrace(); } } } 21、参考代码如下: import java.awt.*; import java.awt.event.*; public class TestButton extends Frame implements ActionListener{ Button jb1,jb2; TextField jt; public TestButton() { super(\ jb1=new Button(\ jb2=new Button(\ jt=new TextField(20); jb1.addActionListener(this); jb2.addActionListener(this); setLayout(new FlowLayout()); add(jt); add(jb1); add(jb2); setSize(250,100); setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource()==jb1){ jt.setText(jb1.getActionCommand()); } if(e.getSource()==jb2){ jt.setText(jb2.getActionCommand()); } 第 20 页 共 32 页 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库《Java程序设计》参考答案1(4)在线全文阅读。
相关推荐: