Gerando arquivo txt

alguem poderia me ajudar com a seguinte questão: como faço pra que o meu programa java gere um arquivo txt? É possível gerar arquivos com a extensão xls tb?

Oi Sigfried, tudo bem? :grin:

Seja bem vindo ao PJ!!! :razz: :razz:

tu pode gerar um txt assim:


import java.io.*;

class criar 
{
     public static void main(String ar[]) throws IOException
     {
          FileOutputStream saida;
          PrintStream fileSaida;
          try
          {
               saida = new FileOutputStream("arquivo.txt");
               fileSaida = new PrintStream(saida);
               fileSaida.print("É... consegui gravar um TXT..");
          }
          catch (Exception e)
          {
               System.err.println(e);
          }
     }
}

agora quanto a xls… não sei te informar!!!

:wink:

Pra gravar um xls ou qualquer outro arquivo que siga um padrão, você vai ter que estudar esse padrão… a menos que existe um programa que faça isso pra você.

Exemplo: como você gravaria um html? Um txt com <html>…

Eu ja gero o txt assim :

[code]import javax.swing.;
import java.io.
;
import java.text.DecimalFormat;

public class Cadastrar {

public Cadastrar() throws IOException {

  String nome&#91;&#93;;
  int sala&#91;&#93;;
  double nota&#91;&#93;&#91;&#93;;
  double media&#91;&#93;;
  
  double total = 0;
  String esc;
    
  nome  = new String&#91;3&#93;;
  sala  = new int&#91;3&#93;;
  nota  = new double&#91;3&#93;&#91;3&#93;;
  media = new double&#91;3&#93;;
  

  /* Cadastro */
  for&#40; int i = 0; i &lt; 3; i++ &#41; &#123;
     nome&#91;i&#93;  = JOptionPane.showInputDialog&#40;
                     &quot;Aluno -&gt; &quot; + &#40;i + 1&#41; +
                     &quot;\nDigite o nome&quot; &#41;;
                     
     sala&#91;i&#93; = Integer.parseInt&#40;
                     JOptionPane.showInputDialog&#40;
                        &quot;Digite a sala&quot; &#41; &#41;;
        
     for&#40; int l = 0; l &lt; 3; l++ &#41; &#123;
        do &#123;
           nota&#91;i&#93;&#91;l&#93; = Double.parseDouble&#40;
                           JOptionPane.showInputDialog&#40;
                              &quot;Digite a &quot; + &#40; l + 1 &#41; +
                              &quot;ª nota da prova&quot; &#41; &#41;;
           if&#40; &#40; nota&#91;i&#93;&#91;l&#93; &gt; 10 &#41; || &#40; nota&#91;i&#93;&#91;l&#93; &lt; 0&#41; &#41; &#123;
              JOptionPane.showMessageDialog&#40; null,
                 &quot;A nota tem q estar entre 0 e 10&quot;, &quot;Valor Inválido !!&quot;,
                 JOptionPane.WARNING_MESSAGE &#41;;
              &#125;
        &#125; while&#40; &#40; nota&#91;i&#93;&#91;l&#93; &gt; 10 &#41; || &#40; nota&#91;i&#93;&#91;l&#93; &lt; 0 &#41; &#41;;

        total += nota&#91;i&#93;&#91;l&#93;;
     &#125; // fim do for das notas
     
     media&#91;i&#93; = total / 3;
     total = 0;
  &#125; // fim do for dos alunos

  // Verifica se o usuario quer salvar a cadastro
  do &#123;
     esc = JOptionPane.showInputDialog&#40; &quot;Deseja Salvar o Cadastro ? &#40;S / N&#41;&quot; &#41;;

     if&#40; esc.equals&#40;&quot;S&quot;&#41; &#41; &#123;
  /************ Nesse IF ele vai gravar os dados no TXT **************/
        DecimalFormat aproximador = new DecimalFormat&#40; &quot;0.0&quot; &#41;;

        FileWriter arquivo = new FileWriter&#40; &quot;Relatorio.txt&quot; &#41;;
        BufferedWriter escritor = new BufferedWriter&#40;arquivo &#41;;

        // Salva as notas no TXT
        for&#40; int k = 0; k &lt; 3; k++ &#41; &#123;
           escritor.write&#40; nome&#91;k&#93; + &quot;\n&quot;&#41;;

           escritor.write&#40; sala&#91;k&#93; + &quot;\n&quot; &#41;;

           for&#40; int i = 0; i &lt; 3; i++ &#41; &#123;
              escritor.write&#40; aproximador.format&#40; nota&#91;k&#93;&#91;i&#93; &#41; + &quot;\n&quot; &#41;;
           &#125;

           escritor.write&#40; aproximador.format&#40; media&#91;k&#93; &#41; + &quot;\n\n&quot; &#41;;
        &#125;// fim do for
        
        escritor.close&#40;&#41;;
        arquivo.close&#40;&#41;;
        
        JOptionPane.showMessageDialog&#40; null, &quot;ARQUIVO SALVO&quot; &#41;;

     &#125; // fim do IF

     // Verifica se o caracter de escolha e S ou N
     if&#40; &#40; !&#40; esc.equals&#40; &quot;S&quot; &#41; &#41; &#41; &amp;&amp; &#40; !&#40;esc.equals&#40; &quot;N&quot; &#41; &#41; &#41; &#41; &#123;
        JOptionPane.showMessageDialog&#40; null,
           &quot;Caracter Inválido&quot;, &quot;ATENÇÃO&quot;,
           JOptionPane.WARNING_MESSAGE &#41;;
     &#125;
  &#125; while&#40; ! &#40; &#40; esc.equals&#40; &quot;S&quot; &#41; &#41; || &#40; esc.equals&#40; &quot;N&quot; &#41; &#41; &#41; &#41;;

} // fim do construtor

} // fim da classe
[/code]

Ja mandei a classe Inteira rssr

e sobre vc fazer o xls, tp… tenta na hora q vc for fazer o font vc mudar na linha

 FileWriter arquivo = new FileWriter&#40; &quot;Relatorio.txt&quot; &#41;;

pra .xls, eu nunca tentei, mas tenta ai!!

espero ter ajudado!!

Abracao e te +