Ler arquivo ate certo determiando tamanho

7 respostas
JJjava

olá,

Por exemplo tenho um arquivo de 10mbs quero ler somente seus primeiros 2 mbs como faria?
estava tantando fazer isso com o StringBuffer para pegar o tamanho q ele esta, mais o metodo tamanho do StringBuffer nao tem anda haver com size me bytes

usando o codigo abaixo para ler o arquivo:

FileReader f_reader = new FileReader(f);
                        BufferedReader buff_reader = new BufferedReader(f_reader);

                        while ((line = buff_reader.readLine()) != null) {

                            b.append(line);
                            
                            b.append("\n");

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("Erro readFile: " + e.getMessage());
                    }
                }

Grato.

7 Respostas

Rodrigo_Sasaki

Não tenho certeza, mas acredito que pode ser feito com RandomAccessFile.

JJjava

será que isso aqui server

// Reads up to len bytes of data from this file into an array of bytes.
read(byte[] b, int off, int len)
kicolobo

Oi JJjava, sua solução não vai te ajudar: este método só indica aonde no array deve ser armazenado o conteúdo do arquivo.

Você pode tentar usar um BufferedReader, lendo um número x de bytes por vez.

Sad7
Veja se isso ajuda amigao:
public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream(new File("D:\\seu_arquivo"));

		// get max length
		int readSize = 2 * 1024 * 1024; // 2Mb
		int len = fis.available() < readSize ? fis.available() : readSize;
		
		byte[] buffer = new byte[len];

		// read to buffer
		fis.read(buffer, 0, len);
		System.out.println(buffer.length);
	}
JJjava
Sad7:
Veja se isso ajuda amigao:
public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream(new File("D:\\seu_arquivo"));

		// get max length
		int readSize = 2 * 1024 * 1024; // 2Mb
		int len = fis.available() < readSize ? fis.available() : readSize;
		
		byte[] buffer = new byte[len];

		// read to buffer
		fis.read(buffer, 0, len);
		System.out.println(buffer.length);
	}

Eu acho q ele so esta lendo se o arquivo tiver 2mb, ao invez de ler os 2 primeiros megas.

Sad7

Não, eu testei em um arquivo de 700Mb, se voce notar, tem um if ternario ali que pega o tamanho maximo de leitura.

// Se o seu arquivo for menor do que seu limite, le até o tamanho do arquivo, senao le ate seu limite (no meu caso coloquei 2mb)
int len = fis.available() < readSize ? fis.available() : readSize;
JJjava

eh compilei denovo o codigo abaixo ai funcionou…

System.out.println("path file " + f.getAbsolutePath());
                        FileInputStream fis = new FileInputStream(f);

                        // get max length  
                        int readSize = 2 * 1024 * 1024; // 2Mb  
                        System.out.println("Read size " + readSize);
                        int len = fis.available() < readSize ? fis.available() : readSize;
                        System.out.println("Len buffer " + len);
                        byte[] buffer = new byte[len];

                        // read to buffer  
                        fis.read(buffer, 0, len);
                        System.out.println("Lenght buffer " + buffer.length);
                        String t = new String(buffer);
                        System.out.println("String " + t);
Criado 30 de agosto de 2012
Ultima resposta 31 de ago. de 2012
Respostas 7
Participantes 4