List of usage examples for java.io File length
public long length()
From source file:DemoPreparedStatementSetAsciiStream.java
public static void main(String[] args) { Connection conn = null;//w w w. jav a 2 s . c o m PreparedStatement pstmt = null; String query = null; try { conn = getConnection(); String fileName = "fileName.txt"; File file = new File(fileName); int fileLength = (int) file.length(); InputStream stream = (InputStream) new FileInputStream(file); query = "insert into LONG_VARCHAR_TABLE(id, stream) values(?, ?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, fileName); pstmt.setAsciiStream(2, stream, fileLength); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = null;// w w w . j av a 2 s . c o m PreparedStatement pstmt = null; Statement stmt = null; ResultSet rs = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); createXMLTable(stmt); File f = new File("build.xml"); long fileLength = f.length(); FileInputStream fis = new FileInputStream(f); String SQL = "INSERT INTO XML_Data VALUES (?,?)"; pstmt = conn.prepareStatement(SQL); pstmt.setInt(1, 100); pstmt.setAsciiStream(2, fis, (int) fileLength); pstmt.execute(); fis.close(); SQL = "SELECT Data FROM XML_Data WHERE id=100"; rs = stmt.executeQuery(SQL); if (rs.next()) { InputStream xmlInputStream = rs.getAsciiStream(1); int c; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((c = xmlInputStream.read()) != -1) bos.write(c); System.out.println(bos.toString()); } rs.close(); stmt.close(); pstmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); // prepare text stream File file = new File("yourFileName.txt"); int fileLength = (int) file.length(); InputStream stream = (InputStream) new FileInputStream(file); pstmt.setString(1, "001"); pstmt.setAsciiStream(2, stream, fileLength); // insert the data pstmt.executeUpdate();/*from w ww. j a va 2s. co m*/ ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(new String(rs.getBytes(2))); } rs.close(); stmt.close(); conn.close(); }
From source file:com.github.xbn.examples.io.non_xbn.SizeOrderAllFilesInDirXmpl.java
public static final void main(String[] ignored) { File fDir = (new File("R:\\jeffy\\programming\\sandbox\\xbnjava\\xbn\\")); Collection<File> cllf = FileUtils.listFiles(fDir, (new String[] { "java" }), true); //Add all file paths to a Map, keyed by size. //It's actually a map of lists-of-files, to //allow multiple files that happen to have the //same length. TreeMap<Long, List<File>> tmFilesBySize = new TreeMap<Long, List<File>>(); Iterator<File> itrf = cllf.iterator(); while (itrf.hasNext()) { File f = itrf.next(); Long LLen = f.length(); if (!tmFilesBySize.containsKey(LLen)) { ArrayList<File> alf = new ArrayList<File>(); alf.add(f);/*from w w w .j a va 2 s . c om*/ tmFilesBySize.put(LLen, alf); } else { tmFilesBySize.get(LLen).add(f); } } //Iterate backwards by key through the map. For each //List<File>, iterate through the files, printing out //its size and path. ArrayList<Long> alSize = new ArrayList<Long>(tmFilesBySize.keySet()); for (int i = alSize.size() - 1; i >= 0; i--) { itrf = tmFilesBySize.get(alSize.get(i)).iterator(); while (itrf.hasNext()) { File f = itrf.next(); System.out.println(f.length() + ": " + f.getPath()); } } }
From source file:Main.java
public static void main(String[] args) throws Exception { File f = new File("hello.txt"); FileReader fr = new FileReader(f); char[] c = new char[(int) f.length()]; char[] cnew = new char[(int) f.length()]; StringBuffer sbuf = new StringBuffer(); fr.read(c, 0, (int) f.length()); int len = (int) f.length(); for (int i = 0, j = len - 1; i < len; i++, j--) { cnew[i] = c[j];//from w w w . ja v a 2 s . c o m sbuf.append(cnew[i]); } System.out.println(sbuf.toString()); fr.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { File f = new File("Main.java"); FileReader fr = new FileReader(f); char[] c = new char[(int) f.length()]; char[] cnew = new char[(int) f.length()]; StringBuffer sbuf = new StringBuffer(); fr.read(c, 0, (int) f.length()); int len = (int) f.length(); for (int i = 0, j = len - 1; i < len; i++, j--) { cnew[i] = c[j];//from w ww . j a va 2s . com sbuf.append(cnew[i]); } System.out.println(sbuf.toString()); fr.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table survey (id int, name BINARY );"); String sql = "INSERT INTO survey (name) VALUES(?)"; PreparedStatement pstmt = conn.prepareStatement(sql); // prepare small binary stream File smallFile = new File("yourFileName.txt"); int smallFileLength = (int) smallFile.length(); InputStream smallStream = (InputStream) new FileInputStream(smallFile); pstmt.setBinaryStream(2, smallStream, smallFileLength); // insert the data pstmt.executeUpdate();// w w w .j av a 2 s . c o m ResultSet rs = stmt.executeQuery("SELECT * FROM survey"); while (rs.next()) { System.out.print(rs.getString(1)); } rs.close(); stmt.close(); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { File file = new File("C:/String.txt"); FileInputStream fin = new FileInputStream(file); byte fileContent[] = new byte[(int) file.length()]; fin.read(fileContent);/*from w w w . j a va 2 s . c om*/ System.out.println(new String(fileContent)); }
From source file:DemoPreparedStatementSetBinaryStream.java
public static void main(String[] args) throws Exception { String smallFileName = "smallFileName.dat"; String largeFileName = "largeFileName.dat"; Connection conn = null;/*from w w w .j a va2s . c om*/ PreparedStatement pstmt = null; try { conn = getConnection(); File smallFile = new File(smallFileName); int smallFileLength = (int) smallFile.length(); InputStream smallStream = (InputStream) new FileInputStream(smallFile); File largeFile = new File(largeFileName); int largeFileLength = (int) largeFile.length(); InputStream largeStream = (InputStream) new FileInputStream(largeFile); String query = "insert into binary_table(id, raw_column, long_raw_column) values(?, ?, ?)"; conn.setAutoCommit(false); pstmt = conn.prepareStatement(query); pstmt.setString(1, "0001"); pstmt.setBinaryStream(2, smallStream, smallFileLength); pstmt.setBinaryStream(3, largeStream, largeFileLength); // execute query, and return number of rows created int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); conn.commit(); } finally { pstmt.close(); conn.close(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("c:\\a.bat"); InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { System.out.println("File is too large"); }/* ww w.j a va 2 s . c om*/ byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (numRead >= 0) { numRead = is.read(bytes, offset, bytes.length - offset); offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); System.out.println(new String(bytes)); }