跳至主要内容

博文

目前显示的是标签为“java”的博文

java copy file (android)

private static void copyFileUsingStream ( File source , File dest ) throws IOException { InputStream is = null ; OutputStream os = null ; try { is = new FileInputStream ( source ); os = new FileOutputStream ( dest ); byte [] buffer = new byte [ 1024 ]; int length ; while (( length = is . read ( buffer )) > 0 ) { os . write ( buffer , 0 , length ); } } finally { is . close (); os . close (); } }

java jdbc mysql

1. 连接数据库   (1) 下载Mysql连接驱动 网址:  http://dev.mysql.com/downloads/connector/j/  ,下载后放在F:\博士科研资料\数据库学习\mysql相关程序文件中,解压。   (2) 加载JDBC驱动 操作方法:在Eclipse中,选中相应的工程,点击Project-Properties中的Java Build Path,在Libraries中增加mysql-connector-java-5.1.21-bin.jar,点OK。   (3) 建一个简单的数据库如下:                    import java.sql.* ; public class GetConnection { public static void main(String[] args){ try { // 调用Class.forName()方法加载驱动程序 Class.forName("com.mysql.jdbc.Driver" ); System.out.println( "成功加载MySQL驱动!" ); } catch (ClassNotFoundException e1){ System.out.println( "找不到MySQL驱动!" ); e1.printStackTrace(); } String url ="jdbc:mysql://localhost:3306/mysql"; // JDBC的URL // 调用DriverManager对象的getConnection()方法,获得一个Connection对象 Connection conn; try { conn = DriverManager.getConnection(url, "root",...

debian install jdk

sudo apt-get install software-properties-common When prompted to confirm the installation, type  y  for yes. To ensure that we get the correct source line on Debian, we’ll need to run the following command that also modifies the line: sudo add-apt-repository "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" Once we do that we’ll need to update: sudo apt-get update Now we’ll go through the installation process of different versions of Java. You can decide which versions you would like to install, and can choose to install one or several. Because it’s the latest stable release, Oracle JDK 8 is the recommended version at the time of writing. Oracle JDK 8 Oracle JDK 8 is the latest stable version of Java at time of writing. You can install it using the following command: sudo apt-get install oracle-java8-installer

Java UDP 广播与多播

摘要: 简单实现了一下基于UDP的广播和多播的功能,主要是理解多播和广播的概念。 1、广播: DatagramSocket.recieve ( DatagramPacket dp ) ; DatagramSocket.send ( DatagramPacket dp ) ; 同一网段所有主机都能接收,前提是端口要监听 客户端发送广播,开启端口监听的服务端接收并打印消息 服务端程序: import  java.io.IOException; import  java.net.DatagramPacket; import  java.net.DatagramSocket; import  java.net.SocketException; public   class  TestServer { public   static   void  main(String[] args) { int  port =  9999 ; //开启监听的端口 DatagramSocket ds =  null ; DatagramPacket dp =  null ; byte [] buf =  new   byte [ 1024 ]; //存储发来的消息 StringBuffer sbuf =  new  StringBuffer(); try  { //绑定端口的 ds =  new  DatagramSocket(port); dp =  new  DatagramPacket(buf, buf.length); System.out. println ( "监听广播端口打开:" ); ds.setSoTimeout(10000); ds.receive(dp); ds.close(); int ...

android java split ArrayIndexOutOfBoundsException

用竖 * 分隔字符串运行将抛出java.util.regex.PatternSyntaxException异常,用加号 + 也是如此。 String[] aa = "aaa*bbb*ccc".split("*" ); // String[] aa = "aaa|bbb|ccc".split("\\*"); 这样才能得到正确的结果 for ( int i = 0 ; i <aa.length ; i++ ) { System.out.println( "--"+ aa[i]); }

android io

Read BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream())); br.readLine( ); Write BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream())); bw.write( bytes[] )

Java中读取字符文件类FileReader

Java - Files and I/O

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, Object, localized characters, etc. Stream A stream can be defined as a sequence of data. there are two kinds of Streams InPutStream:  The InputStream is used to read data from a source. OutPutStream:  the OutputStream is used for writing data to a destination. Java provides strong but flexible support for I/O related to Files and networks but this tutorial covers very basic functionality related to streams and I/O. We would see most commonly used example one by one: Byte Streams Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream  and  FileOutputStream . Following...