⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writefile.java

📁 (文件数据库系统)功能强大,很有价值的代码.
💻 JAVA
字号:
import java.io.*;
import javax.swing.JOptionPane;



public class WriteFile
{
	private RandomAccessFile file;
	private Header h;
	WriteFile(Header header)
	{
		h=header;
		openFile();
	}
		
	
	public  boolean openFile()
	{
	try{String str=new String(h.getName());
		File fileName=new File(str);
		fileName.createNewFile();
		
			file =new RandomAccessFile (fileName+".txt","rw");
		}
		catch(IOException e)
		{
			JOptionPane.showMessageDialog(null,"File does not exist","Invalid File name",
			JOptionPane.ERROR_MESSAGE);
			return false;
		}
		return true;
	}
	
	public void close()
	{
		try{
			if(file!=null)
			file.close();
			}
			catch(IOException e)
			{
				JOptionPane.showMessageDialog(null,"Error closing file","Error",
			JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	}


    
	
	 void update(String key,String[] attri,int[] index)
	{
		
			Record record=new Record(h.getCount()-1);
				int i=0;
			try{
		while(true)
		{
			
	  file.seek(i*record.size());
        record.read(file);StringBuffer bu=new StringBuffer(key);		bu.setLength(15);
        String str=new String(bu).replace('\0',' ');

        if(record.getKey().equals(str))
        {

        	for(int j=0;j<index.length;j++)
        	
        	if(attri[j]!=null)record.setAttri(attri[j],index[j]);	
        	else break; 
System.out.println(record.getAttri(0)+" after");
	  file.seek(i*record.size());

		record.write(file);
		
}
      i++;
        
    
      }
    }
      catch(EOFException e){

      return ;}
      	catch(IOException e)
	 	{System.err.println("in up");
	 	}
    }
    	
	 void update(String conval,int pos,String[] attri,int[] index)
	{
		
			Record record=new Record(h.getCount()-1);
				int i=0;
			try{
		while(true)
		{
			
	  file.seek((i++)*record.size());
        record.read(file);
        if(record.getAttri(pos).trim().equals(conval))
        {	for(int j=0;j<index.length;j++)
        		if(attri[j]!=null)record.setAttri(attri[j],index[j]);
        		else break;
           	  file.seek((i-1)*record.size());
		record.write(file);
		
}

        
    
      }
    }
      catch(EOFException e){

      return ;}
      	catch(IOException e)
	 	{System.err.println("in up");
	 	}
    }
        
   
    
 /*   public addRecord(Record record)
    {
    	try{
    		int key=record.getKey();
    		
    		file.seek((key-1)*Record.size());
    	}
    	catch(IOException e){
    		JOptionPane.showMessageDialog(null,"Error writing to File","Error",JOptionPane.ERROR_MESSAGE);
	
        }
    }
    
    public void save(int keynum,int intf[],String str[])
    {
    	addRecord(getRecord(int keynum,int intf[],String str[]));
    }
  */  
   
    		
    		 void addRecord(String key,String att[])
    		{
    			
			Record record=new Record(h.getCount()-1);
			int i=0;
			try{
		while(true)
		{
			
	  file.seek((i++)*record.size());
        record.read(file);
        if(record.getKey().equals(key)){
        	   JOptionPane.showMessageDialog(null," such Key already exist","Error",JOptionPane.ERROR_MESSAGE);

          return;
        }
        
       }
      }
      catch(EOFException e)
      {      for( i=0;i<att.length;i++)  record.setAttri(att[i],i); record.setKey(key);	
      	record.write(file);

      }
      	catch(IOException e)
	 	{System.err.println("in add");
	 	}
}
/*    
    private void addrecord(Record record)
    {
    	if(record.getKey()!=0)
    	{
    		JOptionPane.showMessageDialog(null,"Record Already exist","Error",JOptionPane.ERROR_MESSAGE);
            return ;
        }
        try{
        	file.seek((record.getKey()-1)*Record.size());
    	}
    	catch(IOException e){
    		JOptionPane.showMessageDialog(null,"Error writing to File","Error",JOptionPane.ERROR_MESSAGE);
	
        }
    }
    private save(int keynum,int intf[],String str[])
    {
    	addRecord(getRecord(keynum,int intf[],String str[]));
    }
    */




	
     void Delete (String key)
    {
    Record record=new Record(h.getCount()-1);
				int i=0;
			try{
		while(true)
		{
			
	  file.seek((i++)*record.size());
        record.read(file);StringBuffer bu=new StringBuffer(key);		bu.setLength(15);
        String str=new String(bu).replace('\0',' ');
        if(record.getKey().equals(str)){record.setKey(null);  for( i=0;i<record.getLength();i++)  record.setAttri(null,i);   	  file.seek((--i)*record.size());
		record.write(file);return;
}
    }
}
catch(EOFException e){
              	   JOptionPane.showMessageDialog(null," no Key already exist","Error",JOptionPane.ERROR_MESSAGE);

      return ;}
      	catch(IOException e)
	 	{System.err.println("in del");
	 	}
    }
void NewTab(Header h1,Header h2)
{
	WriteFile w1=new WriteFile(h1);
	WriteFile w2=new WriteFile(h2);
	
	Record r1=new Record(h1.getCount()-1);
	Record r2=new Record(h2.getCount()-1);
	String name1=h1.getName();
	String name2=h2.getName();
	
				int i=0;
			try{
		while(true)
		{
			
	  w1.file.seek((i++)*r1.size());
        r1.read(w1.file);int j=0;
        try{
        while(true)
        {w2.file.seek((j++)*r2.size());
        r2.read(w2.file);
        Record r=new Record(r1,r2,h1,h2);
        r.write(file);
    }
     }

         catch(EOFException e)
      {    
      System.err.println("end in inner"); 
      break;

      }
      	catch(IOException e)
	 	{System.err.println("in new");
	 	}	
	 		
    }
    	 	System.out.println("after break");

}
    catch(EOFException e)
      {    
      System.err.println("end in outer"); 
      return;

      }
      	catch(IOException e)
	 	{System.err.println("in new");
	 	}	
	 }

void    Select(int index, String[] value)
	{
			
			Record record=new Record((h.getCount()-1)); 
				int i=0;int j=0;
			try{
		while(true)
		{
			
	  file.seek((i++)*record.size());
        record.read(file);
       value[j++]=record.getAttri(index); 
       //System.out.println(record.getAttri(index));  	
}
}  
catch(EOFException e){

      return;}
      	catch(IOException e)
	 	{System.err.println("in sel");
	 	}
	
}
void Select(String []value)
{
		Record record=new Record((h.getCount()-1)); 
				int i=0;int j=0;
			try{
		while(true)
		{
			
	  file.seek((i++)*record.size());
        record.read(file);
       value[j++]=record.getKey();
       }
}  
catch(EOFException e){

      return;}
      	catch(IOException e)
	 	{System.err.println("in sel");
	 	}
	
}

}




        
   	 

    

				

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -