Java IO
 SAMUEL
 SILVINO
UBIRATAN
WELLIGTON
O que é Java IO

Java.IO é uma API (Application Programming
Interface), é um pacote com um conjunto de
classes e subpacotes que são responsáveis pelo
controle de entrada e saída de dados (I/O)
orientada a objeto.
Segundo a Wikipédia , API é um conjunto de
rotinas e padrões estabelecidos por um software
para a utilização das suas funcionalidades por
aplicativos que não pretendem envolver-se em
detalhes da implementação do software, mas
apenas usar seus serviços.
Para que serve


•ler e escrever bytes, caracteres e Strings de/para
a entrada e saída padrão;
• ler e escrever bytes, caracteres e Strings de/para
arquivos;
• utilizar buffers para agilizar a leitura e escrita
através de fluxos;
•usar Scanner e PrintStream.
Implementação


Para a implementação dos códigos de teste com o
java.io utilizaremos o Eclipse, que é o IDE Java
mais utilizado no mundo. Possui como
características marcantes a forte orientação ao
desenvolvimento baseado em plug-ins e o amplo
suporte ao desenvolvedor atendendo as
diferentes necessidades dos programadores.
Criando um Arquivo

   Para criar um arquivo utilizaremos o Menu
File do Eclipse. Clicando com o botão do direito
do mouse em New->File.
    Mas isso poderá ser feito pelo código, o qual
será visto nos slides seguintes.
Lendo um Arquivo

   Para ler um arquivo utilizaremos a classe
abstrata InputStream e as classes concretas
FileInputStream, InputStreamReader,
BufferedReader, e alguns métodos como:
readline(), e close().
Exemplo 1

class Teste{
   public static void main(String[] args) throws IOException {
     InputStream novo = new FileInputStream ("arquivo.txt");
     int recebe = novo.read();
   }
}
Exemplo 2

class Teste{
  public static void main(String[] args) throws IOException {
     InputStream novo = new FileInputStream("arquivo.txt");
     InputStreamReader ler = new InputStreamReader(novo);
     int recebe = ler.read();
  }
}
Exemplo 3

class Teste {
   public static void main(String[] args) throws IOException {
     InputStream arq = new FileInputStream("arquivo.txt");
     InputStreamReader ler = new InputStreamReader(arq);
     BufferedReader buf = new BufferedReader(ler);
     String recebe = buf.readLine();
   }
}
Impressão
class Teste {
  public static void main(String[] args) throws IOException {
     InputStream arq = new FileInputStream("arquivo.txt");
     InputStreamReader ler = new InputStreamReader(arq);
     BufferedReader buf = new BufferedReader(ler);
     String var = buf.readLine();
     while (var != null) {
        System.out.println(var);
        var = buf.readLine();
      }
       buf.close();
   }
}
Escrevendo em um Arquivo


Para escrever em um arquivo primeiro é preciso
ler em Strings do teclado e em seguida convertê-lo
para caractere e depois para bytes e para isso é
necessário a utilização das funções:
BufferedWriter, OutputStreamWriter e
OutputStream.
Exemplo
    class Saida {
     public static void main(String[] args) throws IOException {
       OutputStream saida = new FileOutputStream(“novo.txt");
       OutputStreamWriter escreve = new
       OutputStreamWriter(novo);
       BufferedWriter buf = new BufferedWriter(escreve);

        buf.write(“teste");

        buf.close();
    }
}
Lendo pelo teclado
    class TestaEntrada {
     public static void main(String[] args) throws IOException
{
     InputStream tecd = System.in;
     InputStreamReader ler = new
InputStreamReader(tecd);
     BufferedReader buf = new BufferedReader(ler);
     String var = buf.readLine();

         while (var != null) {
           System.out.println(var);
           var = buf.readLine();
         }
     }
}
Outras formas
Existem outras formas de ler e escrever em
arquivos é uma destas é utilizando o FileWriter eo
FileReader. O FileReader é uma classe utilizada
para ler arquivos e o FileWriter para escrever
nestes.

*Obs: Utilizaremos o CMD e o jEdit/Notepad para
implementar os códigos nos slides seguintes.
Utilizando o FileWriter
import java.io.*;
class TesteEntrada{
  public static void main(String[]args) throws
IOException{
    FileWriter writer = new FileWriter("saida.txt");
    PrintWriter saida = new PrintWriter(writer);
    saida.println("Camera's ready,");

        saida.close();
        writer.close();

    }
}
Utilizando o FileReader
 import java.io.*;
class TesteSaida{
        public static void main(String[]args) throws IOException{
                FileReader reader = new FileReader("saida.txt");
                BufferedReader leitor = new
BufferedReader(reader);
                String linha = null;
                int i=1;
                while((linha = leitor.readLine()) != null) {
                         System.out.println("Linha "+ i+ ": "+ linha);
                         i++;
                }
                leitor.close();
                reader.close();

        }
}
FIM

Apresentação java io

  • 1.
    Java IO SAMUEL SILVINO UBIRATAN WELLIGTON
  • 2.
    O que éJava IO Java.IO é uma API (Application Programming Interface), é um pacote com um conjunto de classes e subpacotes que são responsáveis pelo controle de entrada e saída de dados (I/O) orientada a objeto.
  • 3.
    Segundo a Wikipédia, API é um conjunto de rotinas e padrões estabelecidos por um software para a utilização das suas funcionalidades por aplicativos que não pretendem envolver-se em detalhes da implementação do software, mas apenas usar seus serviços.
  • 4.
    Para que serve •lere escrever bytes, caracteres e Strings de/para a entrada e saída padrão; • ler e escrever bytes, caracteres e Strings de/para arquivos; • utilizar buffers para agilizar a leitura e escrita através de fluxos; •usar Scanner e PrintStream.
  • 5.
    Implementação Para a implementaçãodos códigos de teste com o java.io utilizaremos o Eclipse, que é o IDE Java mais utilizado no mundo. Possui como características marcantes a forte orientação ao desenvolvimento baseado em plug-ins e o amplo suporte ao desenvolvedor atendendo as diferentes necessidades dos programadores.
  • 6.
    Criando um Arquivo Para criar um arquivo utilizaremos o Menu File do Eclipse. Clicando com o botão do direito do mouse em New->File. Mas isso poderá ser feito pelo código, o qual será visto nos slides seguintes.
  • 7.
    Lendo um Arquivo Para ler um arquivo utilizaremos a classe abstrata InputStream e as classes concretas FileInputStream, InputStreamReader, BufferedReader, e alguns métodos como: readline(), e close().
  • 8.
    Exemplo 1 class Teste{ public static void main(String[] args) throws IOException { InputStream novo = new FileInputStream ("arquivo.txt"); int recebe = novo.read(); } }
  • 9.
    Exemplo 2 class Teste{ public static void main(String[] args) throws IOException { InputStream novo = new FileInputStream("arquivo.txt"); InputStreamReader ler = new InputStreamReader(novo); int recebe = ler.read(); } }
  • 10.
    Exemplo 3 class Teste{ public static void main(String[] args) throws IOException { InputStream arq = new FileInputStream("arquivo.txt"); InputStreamReader ler = new InputStreamReader(arq); BufferedReader buf = new BufferedReader(ler); String recebe = buf.readLine(); } }
  • 11.
    Impressão class Teste { public static void main(String[] args) throws IOException { InputStream arq = new FileInputStream("arquivo.txt"); InputStreamReader ler = new InputStreamReader(arq); BufferedReader buf = new BufferedReader(ler); String var = buf.readLine(); while (var != null) { System.out.println(var); var = buf.readLine(); } buf.close(); } }
  • 12.
    Escrevendo em umArquivo Para escrever em um arquivo primeiro é preciso ler em Strings do teclado e em seguida convertê-lo para caractere e depois para bytes e para isso é necessário a utilização das funções: BufferedWriter, OutputStreamWriter e OutputStream.
  • 13.
    Exemplo class Saida { public static void main(String[] args) throws IOException { OutputStream saida = new FileOutputStream(“novo.txt"); OutputStreamWriter escreve = new OutputStreamWriter(novo); BufferedWriter buf = new BufferedWriter(escreve); buf.write(“teste"); buf.close(); } }
  • 14.
    Lendo pelo teclado class TestaEntrada { public static void main(String[] args) throws IOException { InputStream tecd = System.in; InputStreamReader ler = new InputStreamReader(tecd); BufferedReader buf = new BufferedReader(ler); String var = buf.readLine(); while (var != null) { System.out.println(var); var = buf.readLine(); } } }
  • 15.
    Outras formas Existem outrasformas de ler e escrever em arquivos é uma destas é utilizando o FileWriter eo FileReader. O FileReader é uma classe utilizada para ler arquivos e o FileWriter para escrever nestes. *Obs: Utilizaremos o CMD e o jEdit/Notepad para implementar os códigos nos slides seguintes.
  • 16.
    Utilizando o FileWriter importjava.io.*; class TesteEntrada{ public static void main(String[]args) throws IOException{ FileWriter writer = new FileWriter("saida.txt"); PrintWriter saida = new PrintWriter(writer); saida.println("Camera's ready,"); saida.close(); writer.close(); } }
  • 17.
    Utilizando o FileReader import java.io.*; class TesteSaida{ public static void main(String[]args) throws IOException{ FileReader reader = new FileReader("saida.txt"); BufferedReader leitor = new BufferedReader(reader); String linha = null; int i=1; while((linha = leitor.readLine()) != null) { System.out.println("Linha "+ i+ ": "+ linha); i++; } leitor.close(); reader.close(); } }
  • 18.