Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > прочитать vba файл в java


Автор: persten 5.9.2011, 10:38
есть макрос на vba, который пишет файл в произвольном доступе:
Код

Private Type Record
     id As Long
     name As String * 20
     status As String * 10
End Type

Private rec As Record
Private rows_count As Long
Private datfilePath As String

Private Sub writeButton_Click()
    datfilePath = ThisWorkbook.Path + "\data\datfile.dat"
    datfile = FreeFile()
    Open datfilePath For Random As #datfile Len = Len(rec)
    
    rows_count = Int(LOF(datfile) / Len(rec))
    
    rec.id = rows_count + 1
    rec.name = "test_name_" + Str(rows_count + 1)
    rec.status = "test_sta" + Str(rows_count + 1)
    
    Put #datfile, rows_count + 1, rec
    
    rows_count = Int(LOF(datfile) / Len(rec))
    logFile ("rows_count = " + Str(rows_count))
    
    Close #datfile
    
    countLabel.Caption = Str(rows_count)
    
End Sub


как прочитать полученный файл в java?

Автор: LSD 5.9.2011, 11:11
FileInputStream + InputStreamReader (если файл текстовый) в помощь.

Автор: persten 7.9.2011, 21:35
в итоге написал так:
Код

import java.io.*;

class ReadVBFile {
  public static void main(String[] args) throws IOException{ 
    try
    {
      String file = "datfile.dat";
      
      RandomAccessFile fh = new RandomAccessFile(file,"r"); 
      
      int file_length =(int)fh.length();
      int rec_length = 34;
      int rec_count = (int)(file_length/rec_length);
      
      System.out.println("file_length: " + file_length + "\r\n");
      System.out.println("rec_count: " + rec_count + "\r\n");
      
      for( int i_row=0; i_row < rec_count; i_row++ )
      {
        byte[] id_array = new byte[4];
        byte[] name_array = new byte[20];
        byte[] status_array = new byte[10];
        
        for( int i=0; i < 34; i++ )
        {
          byte b = fh.readByte();
          if( i < 4 )
          {
            
            id_array[i] = b;
          }
          else if( i < 24 )
          {
            name_array[i-4] = b;
          }
          else if( i < 34 )
          {
            status_array[i-24] = b;
          }
          
          fh.seek( i_row*34 + i + 1 );
        }
        
        // Long извлекаем как Int
        int myInt = ((id_array[1] & 0xff) << 24) | ((id_array[2] & 0xff) << 16) | ((id_array[3] & 0xff) << 8) | (id_array[0] & 0xff);
        String name_value = new String(name_array);
        String status_value = new String(status_array);
        
        System.out.println( myInt + ", '" + name_value + "', '" + status_value + "'");
      }
    } 
    catch(IOException e)
    {
      System.out.println( "IOException: " + e.getMessage() );
    }
    catch(Exception e)
    {
      System.out.println( "Exception: " + e.getMessage() );
    }    
  }  
}

Автор: priam220 7.9.2011, 21:46
throws IOException не надо

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)