JAVA常用操作语句 项目中的总结六
判断一个文件是否为二进制文件
public static boolean isBinary(File file) {
boolean isBinary = false;
try {
FileInputStream fin = new FileInputStream(file);
long len = file.length();
for (int j = 0; j < (int) len; j++) {
int t = fin.read();
if (t < 32 && t != 9 && t != 10 && t != 13) {
isBinary = true;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return isBinary;
}
将一个student.txt文件的数据导入MySQL数据库中一张student1表中:
import java.io.*;
import java.sql.*;
import java.util.*;
public class TextToDataBase {
/**
* @param args
* 本程序涉及文件IO,字符串分隔StringTokenizer,JDBC,数据库sql语句
*/
public static void main(String[] args) {
Connection con=null;
PreparedStatement pstm=null;
FileReader fr=null;
BufferedReader br=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/exercise1","root","root");
pstm=con.prepareStatement("insert into student1 (ID,name,age,gendar,score) values(?,?,?,?,?)");
fr=new FileReader("D:\\Exercise\\student.txt");
br=new BufferedReader(fr);
for(int i=0;i<5;i++){
String s=br.readLine();
StringTokenizer st=new StringTokenizer(s);
int ID=Integer.parseInt(st.nextToken());
String name=st.nextToken();
int age=Integer.parseInt(st.nextToken());
String gendar=st.nextToken();
int score=Integer.parseInt(st.nextToken());
pstm.setInt(1,ID);
pstm.setString(2,name);
pstm.setInt(3,age);
pstm.setString(4,gendar);
pstm.setInt(5,score);
pstm.executeUpdate();
}
br.close();
pstm.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
相关文档:
Java里有个很重要的特色是Exception ,也就是说允许程序产生例外状况。而在学Java 的时候,我们也只知道Exception 的写法,却未必真能了解不同种类的Exception 的区别。
首先,您应该知道的是Java 提供了两种Exception 的模式,一种是执行的时候所产生的Exception (Runtime Exception),另外一种则是受控制的Exception ......
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author troy(J2EE)
* @version 1.0
*/
public class Test {
public static void main(String[] args) throws Exception {
DateFormat df = DateFormat.getDateInstance();
&n ......
本文主要谈一下密码学中的加密和数字签名,以及其在java中如何进行使用。对密码学有兴趣的伙伴,推荐看Bruce Schneier的著作:Applied Crypotography.在jdk1.5的发行版本中安全性方面有了很大的改进,也提供了对RSA算法的直接支持,现在我们从实例入手解决问题(本文仅是作为简单介绍):
一、密码学上 ......
由于有个合作项目,用到了REST,我们这边的服务器是java的,合作方那边主要是PHP环境,为了远程调用的问题,使用了REST作为API的实现方
案。现在项目做得差不多了,下面记下自己的一点心得,算是笔记吧。
REST(Representational State Transfer)的说法来自“Architectural Styles and the Desi ......
/*
* GetMacAddress .java
*
* description:get Mac addreess
*
* @author hadeslee
*
* Created on 2007-9-27, 9:11:15
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test2;
import java.io.BufferedReader;
import java.io.IO ......