Java编程精简版

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

IO编程:

package cn.sdut;

import java.io.*;

public class Main {

public static void main(String[] args) throws Exception { FileInputStream fis=new FileInputStream("c:/test/aa.txt"); FileOutputStream fos=new FileOutputStream("c:/test/gg.txt"); BufferedInputStream bis=new BufferedInputStream(fis); BufferedOutputStream bos=new BufferedOutputStream(fos); //一个字节为单位读写

int b=0;

while((b=bis.read())!=-1)

{

//System.out.println(b);

bos.write(b);

}

*/

//1k为单位读写

int len=0;

byte[] bytes=new byte[1024];

while((len=bis.read(bytes))!=-1)

{

bos.write(bytes,0,len);

}

System.out.println(l2-l1);

bis.close();

bos.close();

fis.close();

fos.close();

}

}

BufferedReader 和 BufferedWriter按行读一次一行

package cn.sdut;

import java.io.*;

public class Main {

public static void main(String[] args) throws IOException { FileReader reader=new FileReader("c:/test/aa.txt");

FileWriter writer=new FileWriter("c:/test/aa5.txt");

BufferedReader br=new BufferedReader(reader);

BufferedWriter bw=new BufferedWriter(writer);

String str=null;

while((str=br.readLine())!=null)

{

bw.write(str);

bw.newLine();

}

br.close();

bw.close();

reader.close();

writer.close();

}

多线程编程:第一个代码为每个人卖20张票,第二个为一共卖20张票

class MyThread extends Thread{

private int ticket = 20;

private String name;

public MyThread(String name){

=name;

}

public void run(){

for(int i =0;i<20;i++){

if(this.ticket>0){

System.out.println(+"卖"+(this.ticket--)+"号票");

}

}

}

}

public class sss {

public static final int sum=0;

public static void main(String[] args) {

MyThread mt1= new MyThread("一号窗口");

MyThread mt2= new MyThread("二号窗口");

MyThread mt3= new MyThread("三号窗口");

mt1.start();

mt2.start();

mt3.start();

}

}

class MyThread1 implements Runnable{

private int ticket =20;

public void run(){

for(int i =0;i<100;i++){

if(this.ticket>0){

System.out.println(Thread.currentThread().getName()+"卖"+(this.ticket--)+"号票");

}

}

}

}

public class test {

public static void main(String[] args) {

// TODO Auto-generated method stub

MyThread1 mt = new MyThread1();

Thread t1 = new Thread(mt,"一号窗口");

Thread t2 = new Thread(mt,"二号窗口");

Thread t3 = new Thread(mt,"三号窗口");

t1.start();

t2.start();

t3.start();

}

}

JDBC 对记录进行增删改查——Statement----MySQL

package cn.sdut;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Arrays;

public class Main {

public static void main(String[] args) throws ClassNotFoundException, SQLException { //1 加载驱动

Class.forName("com.mysql.jdbc.Driver");

//2 得到数据库的连接

Connection con = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8",

"root",

"");

//3 SQL语句执行对象

//创建SQL语句执行对象

Statement st = con.createStatement();

//增加记录

//String insertSQL="insert into student values(null,’李四’,’2000-01-01’,44),(null,’李四2’,’2001-02-03’,66)";

//int result1=st.executeUpdate(insertSQL);

//删除记录

/*String delSQL="delete from student where name=’李四’";

int result2=st.executeUpdate(delSQL);*/

//修改记录

String updateSQL="update student set score=score+30 where name=’李四2’and score=55.0";

int result3=st.executeUpdate(updateSQL);

System.out.println(result3);

//查询所有记录

String sql="select * from student";

相关文档
最新文档