[九]java作業

文件拷貝

import java.io.*;

public class copyfiles {

	public static void main(String[] args) {
		File infile=new File("001.txt");
		File outfile=new File("002.txt");
		try{
			outfile.createNewFile();
			InputStream in=new FileInputStream(infile);
			OutputStream out=new FileOutputStream(outfile);
			for(int tmp;(tmp=in.read())!=-1;){
				out.write(tmp);
			}
			System.out.printf("ok");
			in.close();
			out.close();
		}catch(IOException e){
			e.printStackTrace();
			return ;
		}
	}
}

LineNumberReader讀文件

import java.io.*;

public class linenum {

	public static void main(String[] args) {
		try{
		FileReader input=new FileReader("003.txt");
		LineNumberReader inline=new LineNumberReader(input);
		for(String str;(str=inline.readLine())!=null;){
			System.out.println(str);
		}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

文件隨機插入100個隨機串

import java.io.*;

public class srandstr {
	private int[][] num;
	public srandstr(){
		num=new int[100][2];
	}
	public void creat8Gfiles(){
		try{
			FileWriter writer=new FileWriter("004.txt");
			BufferedWriter buffer=new BufferedWriter(writer);
			StringBuilder[] str=new StringBuilder[20];
			int time=128*1024;
			System.out.printf("initialize:\n");
			for(int j=0;j<20;j++){
				str[j]=new StringBuilder();
				for(int k=0;k<1024;k++){
					str[j].append((int)(Math.random()*10));
				}
			}
			System.out.printf("write in:\n");
			long start=System.currentTimeMillis();
			for(int j=0;j<64;j++){
				for(int i=0;i<time;i++){
					buffer.write(str[(int)(Math.random()*20)].toString());
				}
				System.out.printf(" %02d%%\n",j*100/64);
			}
			System.out.printf("100%%\n");
			buffer.flush();
			buffer.close();
			long end=System.currentTimeMillis();
			int sec =(int)(end-start)/1000%60;
			int mine=(int)(end-start)/1000/60%60;
			int hour=(int)(end-start)/1000/3600;
			
			System.out.printf("finish:%02d:%02d:%02d\n",hour,mine,sec);
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	private boolean checksame(int n,int index){
		for(int i=0;i<index;i++){
			if(n==num[i][0]){
				return true;
			}
		}
		return false;
	}
	public void insertstr(){
		try{
			System.out.println("insert str:");
			RandomAccessFile input=new RandomAccessFile("004.txt", "rw");
			String[] str=new String[100];
			long maxid = input.length()/128;
			for(int j=0;j<100;j++){
				str[j]=new String();
				for(int k=0;k<128;k++){
					str[j]+=((int)(Math.random()*26+'A'));
				}
				byte[] tmp=str[j].getBytes("GB2312");
				num[j][1]=tmp.length;
				System.out.println((j+1)+":"+str[j]);
				for(;checksame(num[j][0],j);){
					num[j][0]=(int)(Math.random()*maxid);
				}
				input.seek(num[j][0]);
				input.writeBytes(str[j]);
			}
			input.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	public void readstr(){
		try{
			System.out.println("read str:");
			RandomAccessFile output=new RandomAccessFile("004.txt", "r");
			String str;
			for(int j=0;j<100;j++){
				byte[] tmp=new byte[num[j][1]];
				output.seek(num[j][0]);
				output.read(tmp);
				str=new String(tmp);
				System.out.printf((j+1)+":"+str);
			}
			output.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		srandstr tmp=new srandstr();
		tmp.creat8Gfiles();
		tmp.insertstr();
		tmp.readstr();
	}
}

內部類

public class innerclass {
	private String schoolname;
	class academy{
		private String name;
		private String location;
		public void name(String str){
			this.name=str;
		}
		public String name(){
			return this.name;
		}
		public void location(String str){
			this.location=str;
		}
		public String location(){
			return this.name;
		}
	}
	public void schoolname(String str){
		this.schoolname=str;
	}
	public String schoolname(){
		return this.schoolname;
	}
	public static void main(String[] args) {
	}
}


創建界面

import java.awt.*;

import javax.swing.*;

public class zxc {
	public static void main(String[] args) {
		JFrame frame = new JFrame();  
        JPanel panel = new JPanel();  
        JTextArea textArea = new JTextArea();
        JTextArea textArea2 = new JTextArea();
          
        panel.setLayout(new GridLayout());  
        textArea.setText("test");
        textArea2.setText("test2");
        panel.add(new JScrollPane(textArea));  
        panel.add(new JScrollPane(textArea2));
        frame.add(panel);
          
        frame.setSize(400,200);
        Dimension frameSize = frame.getSize();
        Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((displaySize.width-frameSize.width)/2,(displaySize.height - frameSize.height) / 2);
        frame.setVisible(true); 
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章