Java复习之知识点整理(十六)--- GUI(图形用户接口),多线程读取大文本文件

public class MyTestWindow implements ActionListener {
	
	JFrame win;
	JTextArea txt;
	JButton saveBtn;
	JButton openBtn;
	
	public static long size = 0;
	
	
	public static void main(String[] args) {
		new MyTestWindow();
	}
	
	
	public MyTestWindow()
	{
		//初始化窗口
		win = new JFrame("文件编辑器");
		win.setLayout(null);
		win.setBounds(100, 100, 1000, 800);
			
		//初始化文字区域
		txt = new JTextArea();
		txt.setBounds(0, 0, 800 , 500);		
		win.add(txt);
		
		
		//文字区域添加滚动条
		JScrollPane js = new JScrollPane(txt);
		js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		js.setVisible(true);
		js.setBounds(0, 0, 900 , 500);	
		win.add(js);
		
		
		//初始化打开按钮
		openBtn = new JButton("打开");
		openBtn.setBounds(200, 600, 200 , 50);		
		openBtn.addActionListener(this);
		win.add(openBtn);
		
		//初始化保存按钮
		saveBtn = new JButton("保存");
		saveBtn.setBounds(600, 600, 200 , 50);		
		saveBtn.addActionListener(this);
		win.add(saveBtn);
		
		win.addWindowListener(new WindowAdapter() {


			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(-1);
			}
			
			
		});
		
		win.setVisible(true);
	}




	@Override
	public void actionPerformed(ActionEvent e) {
		
		//如果点击打开按钮
		if(e.getSource() == openBtn)
		{
			
			btnOpen();
			
//			try {
//				
//				FileDialog fd = new FileDialog(win, "打开文件");
//				fd.setVisible(true);
//				
//				//System.out.println(fd.get);
//				File f = new File(fd.getDirectory(),fd.getFile());
//				FileInputStream fis = new FileInputStream(f);
//				InputStreamReader isr = new InputStreamReader(fis,"utf-8");				
//				char [] cbuf = new char [1024];
//				int len = 0;
//				while((len = isr.read(cbuf))!= -1)
//				{
//					String str = new String (cbuf);
//					txt.append(str);
//				}
//				isr.close();
//				fis.close();
//				
//			} catch (Exception e2) {
//				// TODO: handle exception
//			}
			
		}
		//如果是保存按钮
		else if(e.getSource() == saveBtn)
		{
			
			btnSave();
			
			//String str = txt.getText();
//			try {
//				
//				FileOutputStream fos = new FileOutputStream("D:\\a.txt");
//				OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
//				
//				//FileWriter fw = new FileWriter("D:\\a.txt");
//				//fw.write(str);
//				//fw.close();
//				osw.write(str);
//				osw.close();
//				fos.close();
				
//			} catch (Exception e2) {
//				// TODO: handle exception
//			}
		}
		
	}
	
	/**
	 * 点击打开按钮
	 */
	private void btnOpen()
	{
		JFileChooser jf = new JFileChooser("d:/");
		int value = jf.showDialog(win, "打开文件");
		if(value == JFileChooser.APPROVE_OPTION)
		{
			File getPath = jf.getSelectedFile(); //取得路径
			//readBigFileByThread(getPath,txt);
			//readBigFileByThread_1(getPath,txt);
			new ThreadTs(getPath,txt).start();
		}
		else
		{
			
		}
		
		
	}
	
	
	/**
	 * 点击保存按钮
	 */
	private void btnSave()
	{
		JFileChooser jf = new JFileChooser("d:/");
		int value = jf.showSaveDialog(null);


		if(value == JFileChooser.APPROVE_OPTION)
		{ 
			//判断窗口是否点的是打开或保存		 
			File getPath = jf.getSelectedFile(); //取得路径
			//
			String str = txt.getText();
			try {
				
				FileWriter fw = new FileWriter(getPath);
				fw.write(str);
				fw.flush();
				fw.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{			
		}	
	}
	
	
	/**
	 * 多线程读取大文本文件
	 * @param srcPath
	 */
	private void readBigFileByThread(File srcFile,JTextArea txt)
	{
		//线程数
		int threadCount = 5;
		//字节总量
		long totleLen = srcFile.length();
		//分块
		long blocks = totleLen / threadCount;
		//分块之后剩余
		long remain = totleLen % threadCount;
		//随机输入流
		try 
		{
			//循环次数
			for (int i = 0; i < threadCount; i++) 
			{			
				RandomAccessFile srcRaf = new RandomAccessFile(srcFile, "r");	
				Thread t = null;
				//最后一次循环,要加上剩余
				if((i == threadCount -1))
				{	
					t = new ReadTherad(blocks + remain , i * blocks ,srcRaf,txt);					
				}
				else
				{
					t = new ReadTherad(blocks, i * blocks ,srcRaf,txt);										
				}
				t.start();
			}
		}
		catch (Exception e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
	
		}
	}
			
}




class ThreadTs extends Thread
{
	//随机流
	private File srcFile;	
	private JTextArea txt;
	
	public File getSrcPath() {
		return srcFile;
	}
	public void setSrcPath(File srcPath) {
		this.srcFile = srcPath;
	}
	
	public JTextArea getTxt() {
		return txt;
	}
	public void setTxt(JTextArea txt) {
		this.txt = txt;
	}
	
	public ThreadTs(File srcPath, JTextArea txt) {
		super();
		this.srcFile = srcPath;
		this.txt = txt;
	}
	
	public void run()
	{
		try {
			
			FileReader fr = new FileReader(srcFile);
			char [] cbuf = new char [1024];
			int len = 0;
			
			while((len = fr.read(cbuf)) != -1)
			{
				String str = new String(cbuf,0,len);
				txt.append(str);
			}
			fr.close();
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}	
}








class ReadTherad extends Thread
{
	//本次读取的字节数
	private long blockSize;
	//本次的游标
	private long seek;
	//随机流
	private RandomAccessFile srcRaf;
	
	private JTextArea txt;
	
	public ReadTherad(long blockSize, long seek, RandomAccessFile srcRaf) {
		this.blockSize = blockSize;
		this.seek = seek;
		this.srcRaf = srcRaf;
	}




	public ReadTherad(long blockSize, long seek, RandomAccessFile srcRaf,
			JTextArea txt) {
		super();
		this.blockSize = blockSize;
		this.seek = seek;
		this.srcRaf = srcRaf;
		this.txt = txt;
	}




	public long getBlockSize() {
		return blockSize;
	}




	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}




	public long getSeek() {
		return seek;
	}




	public void setSeek(int seek) {
		this.seek = seek;
	}




	public RandomAccessFile getSrcRaf() {
		return srcRaf;
	}




	public void setSrcRaf(RandomAccessFile srcRaf) {
		this.srcRaf = srcRaf;
	}


	
	public void run()
	{
		try {		
			srcRaf.seek(seek);	
			byte [] buf = new byte [1024];			
			long count = blockSize / buf.length; 
			int remain = (int)(blockSize % buf.length);
			
			for (int i = 0; i < count; i++) {
				srcRaf.read(buf);
				String s = new String(buf);
				txt.append(s);
				MyTestWindow.size += buf.length;
				System.out.println(MyTestWindow.size);
			}
			srcRaf.read(buf,0,remain);
			String s = new String(buf,0,remain);
			txt.append(s);
			MyTestWindow.size += remain;
			System.out.println(MyTestWindow.size);
			srcRaf.close();
				
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

猜你喜欢

转载自blog.csdn.net/xcvbxv01/article/details/80935324