77范文网 - 专业文章范例文档资料分享平台

java 实验报告(2)

来源:网络收集 时间:2020-02-21 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

Vector allWord,noSameWord; File file=new File(\ Scanner sc=null; String regex; WordStatistic(){ allWord=new Vector(); noSameWord=new Vector();//regex是由空格·数字和符号(!\组成的正则表达式 regex=\ try{ sc=new Scanner(file);//创建指向file的sc sc.useDelimiter(regex); //sc调用useDelimiter(String regex)方法,向参数传递regex } catch(IOException exp){ System.out.println(exp.toString()); } } void setFileName(String name){ file=new File(name); try{ sc=new Scanner(file); sc.useDelimiter(regex); } catch(IOException exp){ System.out.println(exp.toString()); } } public void wordStatistic(){ try{ while(sc.hasNext()){ String word=sc.next(); allWord.add(word); if(!noSameWord.contains(word)) noSameWord.add(word); } } catch(Exception e){} } public Vector getAllWord(){ return allWord; } public Vector getNoSameWord(){ return noSameWord;

} }

三、结果截图:

四、实验分析:

按字典顺序输出全部不相同的单词。 答:

Dictionary.java import java.util.*;

public class Dictionary{

public static void main(String args[]){ Vector allWord,noSameWord;

WordStatistic statistic=new WordStatistic(); statistic.setFileName(\

statistic.wordStatistic();//statistic调用WordStatistic()方法 allWord=statistic.getAllWord();

noSameWord=statistic.getNoSameWord();

System.out.println(\共有\个英文单词\

System.out.println(\有\个互不相同英文单词\ System.out.println(\按字典顺序排列:\ String s[]=new String [noSameWord.size()]; for(int i=0;i

Arrays.sort(s);

for(int i=0;i

实验3 读取压缩文件

一、实验目的:掌握ZipInputStream流的使用。 二、实验代码: import java.io.*;

import java.util.zip.*;

public class ReadZipFile {

public static void main(String args[]) { File f=new File(\ File dir=new File(\ byte b[]=new byte[100]; dir.mkdir(); try{

ZipInputStream in=new ZipInputStream(new FileInputStream(f)); ZipEntry zipEntry=null;

while((zipEntry=in.getNextEntry())!=null) { File file=new File(dir,zipEntry.getName());

FileOutputStream out=new FileOutputStream(file); int n=-1;

System.out.println(file.getAbsolutePath()+\的内容:\ while((n=in.read(b,0,100))!=-1) { String str=new String(b,0,n); System.out.println(str);

out.write(b,0,n); }

out.close(); }

in.close(); }

catch(IOException ee) { System.out.println(ee); } } }

三、结果截图:

四、实验分析:

编写一个GUI程序,提供一个对话框,用户可以使用这个对话框选择要解压缩的Zip文件,设置解压后所得到的文件的存放目录。 答:

import java.io.*;

import java.util.zip.*; import java.awt.*;

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

public class MyFrame extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private JFileChooser fileChooser = new JFileChooser(\ private JButton button = new JButton(\打开想要解压文件\ public MyFrame() { this.setTitle(\解压\ this.setPreferredSize(new Dimension(500, 400)); this.getContentPane().add(button, BorderLayout.CENTER); button.addActionListener(this);

this.pack(); this.setLocationRelativeTo(null); this.setIgnoreRepaint(true); }

public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source instanceof JButton) { openFile(); } }

public void openFile() { fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle(\打开Zip文件夹\ int ret = fileChooser.showOpenDialog(null); if (ret == JFileChooser.APPROVE_OPTION) { System.out.println(fileChooser.getSelectedFile().getAbsolutePath()); File f=new File(\ File dir=new File(\ byte b[]=new byte[100]; dir.mkdir(); try{ZipInputStream in=new ZipInputStream(new FileInputStream(f)); ZipEntry zipEntry=null; while((zipEntry=in.getNextEntry())!=null){ File file=new File(dir,zipEntry.getName()); FileOutputStream out=new FileOutputStream(file); int n=-1; System.out.println(file.getAbsolutePath()+\的内容:\ while((n=in.read(b,0,100))!=-1){ String str=new String(b,0,n); System.out.println(str); out.write(b,0,n); } out.close(); } in.close(); } catch(IOException ee){ System.out.println(ee); } } }

public static void main(String[] args) { Frame frame = new MyFrame(); frame.setVisible(true);

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库java 实验报告(2)在线全文阅读。

java 实验报告(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/770217.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: