Advertisement
Guest User

GravaLinhas

a guest
Sep 9th, 2011
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. /*Objetivo desse script Java grava linhas em um arquivo informado via linha de comando até que seja digitado . para encerrar
  2. @autor: Reginaldo
  3. compilar: javac GravaLinhas.java
  4. executar: java nomearquivo*/
  5. import java.io.*;
  6. import java.util.Scanner;
  7. public class GravaLinhas{
  8.     static File arquivo;
  9.     static String texto;
  10.         public static void main(String[] args){
  11.          Scanner entrada = new Scanner(System.in);
  12.             if(args.length < 1){
  13.                 System.out.println("Utilização: java GravaLinhas <arquivo>");
  14.                 return;
  15.             }
  16.             try{
  17.                 arquivo = new File(args[0]);
  18.                 FileOutputStream fos = new FileOutputStream(arquivo);
  19.                 do{
  20.                       texto = entrada.nextLine(); //lê do teclado
  21.                       if(!texto.equals("."))
  22.                         fos.write(texto.getBytes()); //grava  no arquivo
  23.                      
  24.                 }while(!texto.equals("."));
  25.                     fos.close();
  26.             }catch(IOException e){
  27.                 e.printStackTrace();
  28.             }
  29.         }
  30.    
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement