Só lê uma coluna do arquivo txt?

1 resposta
D

Galera, preciso de uma ajuda, vou colocar várias dúvidas de uma vez, estou precisando ler um arquivo texto fazer algumas formatações e gravar em outro arquivo txt, isso já estou fazendo, só que não estou sabendo ler os dados, pois só conssigo pegar a primeira coluna, ou melhor sempre os dados da primeira linha seria isso??, se não estiver enganado pois, em arquivo não le em colunas e sim em linha não é isso??

Bem segue parte do código:

....
String arquivoFonte = "saida.txt";
PrintWriter saida = new PrintWriter(new FileWriter(arquivoFonte));
						
while((linha = leitor.readLine())!= null){
				
StringTokenizer st = new StringTokenizer(linha);
String dados = st.nextToken();
				
valorAchado = dados.length();
valorFinalAchado = valorMaximo - valorAchado;

for(int i = 0; i < valorFinalAchado;i++){
dados = dados.concat(" ");
				}
				
saida.println(dados);//grava no arquivo o codigo
....

Outa dúvida seria na gravação do arquivo, está certo essa maneira, saida.println(), pois achei que seria saida.write(), mais ai ele não escreve em coluna e sim em linhas??

Meu arquivo está formatado com <TAB> entre as colunas, ai que vem a dúvida principal como pego o primero registro e faço o tratamento nele, depois pego o segundo e faço o tratamento nele e assim vai…

agradeço qualquer ajuda urgente!!!

Um abração!! :?: :roll:

1 Resposta

T

Se puder pegar um livro em português que fala sobre isso é mais fácil. Senão, está aqui o Javadoc:

A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.

A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

The following is one example of the use of the tokenizer. The code:

StringTokenizer st = new StringTokenizer("this is a test");
     while (st.hasMoreTokens()) {
         System.out.println(st.nextToken());
     }

prints the following output:

this
     is
     a
     test

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

String[] result = "this is a test".split("\s");
     for (int x=0; x<result.length; x++)
         System.out.println(result[x]);

prints the following output:

this
     is
     a
     test

>

Criado 5 de agosto de 2005
Ultima resposta 5 de ago. de 2005
Respostas 1
Participantes 2