Java by API/java.io/FileInputStream — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:32, 31 мая 2010

FileInputStream: available()

 
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:databaseName";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      FileInputStream fis = new FileInputStream("somefile.txt");
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement createTable = connection.createStatement();
      createTable.executeUpdate("CREATE TABLE source_code (name CHAR(20), source LONGTEXT)");
      String ins = "INSERT INTO source_code VALUES(?,?)";
      PreparedStatement statement = connection.prepareStatement(ins);
      statement.setString(1, "TryInputStream"); // Set first field
      statement.setAsciiStream(2, fis, fis.available()); // Stream is source
      int rowsUpdated = statement.executeUpdate();
      System.out.println("Rows affected: " + rowsUpdated);
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}





FileInputStream: close()

 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");
    if (!file1.exists() || !file2.exists()) {
      System.out.println("One or both files do not exist");
      System.out.println(false);
    }
    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
      System.out.println("lengths not equal");
      System.out.println(false);
    }
    try {
      FileInputStream fis1 = new FileInputStream(file1);
      FileInputStream fis2 = new FileInputStream(file2);
      int i1 = fis1.read();
      int i2 = fis2.read();
      while (i1 != -1) {
        if (i1 != i2) {
          areFilesIdentical = false;
          break;
        }
        i1 = fis1.read();
        i2 = fis2.read();
      }
      fis1.close();
      fis2.close();
    } catch (IOException e) {
      System.out.println("IO exception");
      areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
  }
}





FileInputStream: getChannel()

 
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String args[]) {
    FileInputStream fIn;
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;
    try {
      fIn = new FileInputStream("test.txt");
      fChan = fIn.getChannel();
      fSize = fChan.size();
      mBuf = ByteBuffer.allocate((int) fSize);
      fChan.read(mBuf);
      mBuf.rewind();
      for (int i = 0; i < fSize; i++)
        System.out.print((char) mBuf.get());
      fChan.close();
      fIn.close();
    } catch (IOException exc) {
      System.out.println(exc);
    }
  }
}





FileInputStream: getFD()

 
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] a) {
    File aFile = new File("C:/myFile.text");
    FileInputStream inputFile1 = null; 
    FileDescriptor fd = null; 
    try {
      inputFile1 = new FileInputStream(aFile);
      fd = inputFile1.getFD(); 
    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
    FileInputStream inputFile2 = new FileInputStream(fd);
  }
}





FileInputStream: read()

 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");
    if (!file1.exists() || !file2.exists()) {
      System.out.println("One or both files do not exist");
      System.out.println(false);
    }
    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
      System.out.println("lengths not equal");
      System.out.println(false);
    }
    try {
      FileInputStream fis1 = new FileInputStream(file1);
      FileInputStream fis2 = new FileInputStream(file2);
      int i1 = fis1.read();
      int i2 = fis2.read();
      while (i1 != -1) {
        if (i1 != i2) {
          areFilesIdentical = false;
          break;
        }
        i1 = fis1.read();
        i2 = fis2.read();
      }
      fis1.close();
      fis2.close();
    } catch (IOException e) {
      System.out.println("IO exception");
      areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
  }
}





new FileInputStream(FileDescriptor fdObj)

 
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
  public static void main(String[] a) {
    File aFile = new File("C:/myFile.text");
    FileInputStream inputFile1 = null; 
    FileDescriptor fd = null; 
    try {
      inputFile1 = new FileInputStream(aFile);
      fd = inputFile1.getFD(); 
    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
    FileInputStream inputFile2 = new FileInputStream(fd);
  }
}





new FileInputStream(File file)

 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Main {
  public static void main(String[] a) {
    File aFile = new File("C:/myFile.txt");
    FileInputStream inputFile = null;
    try {
      inputFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
  }
}





new FileInputStream(String name)

 
/*
 * Output:
 */
import java.io.DataInputStream;
import java.io.FileInputStream;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileInputStream fis = new FileInputStream("fileName.dat");
      // Create a data input stream
      DataInputStream dis = new DataInputStream(fis);
      // Read and display data
      System.out.println(dis.readBoolean());
      System.out.println(dis.readByte());
      System.out.println(dis.readChar());
      System.out.println(dis.readDouble());
      System.out.println(dis.readFloat());
      System.out.println(dis.readInt());
      System.out.println(dis.readLong());
      System.out.println(dis.readShort());
      // Close file input stream
      fis.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}