package threadgroup;

創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!專注中小微企業(yè)官網(wǎng)定制,成都做網(wǎng)站、網(wǎng)站制作,塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。
class ThreadDemo3 extends Thread {
private String name;
private int delay;
public ThreadDemo3(String sname, int i_delay) {
name = sname;
delay = i_delay;
}
public void run() {
try {
sleep(delay);
} catch (InterruptedException e) {
}
System.out.println("多線程測試!\n" + name + "\n" + delay);
}
}
public class testMyThread {
public static void main(String[] args) {
ThreadDemo3 th1,th2,th3;
th1 = new ThreadDemo3("線程1", (int) (Math.random() * 900));
th2 = new ThreadDemo3("線程2", (int) (Math.random() * 900));
th3 = new ThreadDemo3("線程3", (int) (Math.random() * 900));
th1.start();
th2.start();
th3.start();
}
}
package threadgroup;
public class threadDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("你好嗎?");
System.out.println("正在進(jìn)行的Thread是:" + t);
try {
for (int i = 0; i 5; i++) {
System.out.println("我不叫穆繼超" + i);
Thread.sleep(3000);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
}
}
package threadgroup;
public class threadDemo2 implements Runnable {
public threadDemo2() {
Thread t1 = Thread.currentThread();
t1.setName("第一個(gè)主進(jìn)程");
System.out.println("正在運(yùn)行" + t1);
Thread t2 = new Thread(this, "");
System.out.println("在創(chuàng)建一個(gè)進(jìn)程");
t2.start();
try {
System.out.println("使他進(jìn)入第一個(gè)睡眠狀態(tài)");
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第一個(gè)進(jìn)程");
}
public void run() {
try {
for (int i = 0; i 5; i++) {
System.out.println("進(jìn)程" + i);
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// TODO: handle exception
System.out.println("Thread has wrong" + e.getMessage());
}
System.out.println("退出第二個(gè)進(jìn)程");
}
public static void main(String[] args) {
new threadDemo2();
}
}
package IO;
import java.io.*;
public class FileDirectoryDemo {
public static void main(String[] args) {
// 如果沒有指定參數(shù),則缺省為當(dāng)前目錄。
if (args.length == 0) {
args = new String[] { "." };
}
try {
// 新建指定目錄的File對象。
File currentPath = new File(args[0]);
// 在指定目錄新建temp目錄的File對象。
File tempPath = new File(currentPath, "temp");
// 用“tempPath”對象在指定目錄下創(chuàng)建temp目錄。
tempPath.mkdir();
// 在temp目錄下創(chuàng)建兩個(gè)文件。
File temp1 = new File(tempPath, "temp1.txt");
temp1.createNewFile();
File temp2 = new File(tempPath, "temp2.txt");
temp2.createNewFile();
// 遞歸顯示指定目錄的內(nèi)容。
System.out.println("顯示指定目錄的內(nèi)容");
listSubDir(currentPath);
// 更改文件名“temp1.txt”為“temp.txt”。
File temp1new = new File(tempPath, "temp.txt");
temp1.renameTo(temp1new);
// 遞歸顯示temp子目錄的內(nèi)容。
System.out.println("更改文件名后,顯示temp子目錄的內(nèi)容");
listSubDir(tempPath);
// 刪除文件“temp2.txt”。
temp2.delete();
// 遞歸顯示temp子目錄的內(nèi)容。
System.out.println("刪除文件后,顯示temp子目錄的內(nèi)容");
listSubDir(tempPath);
} catch (IOException e) {
System.err.println("IOException");
}
}
// 遞歸顯示指定目錄的內(nèi)容。
static void listSubDir(File currentPath) {
// 取得指定目錄的內(nèi)容列表。
String[] fileNames = currentPath.list();
try {
for (int i = 0; i fileNames.length; i++) {
File f = new File(currentPath.getPath(), fileNames[i]);
// 如果是目錄,則顯示目錄名后,遞歸調(diào)用,顯示子目錄的內(nèi)容。
if (f.isDirectory()) {
// 以規(guī)范的路徑格式顯示目錄。
System.out.println(f.getCanonicalPath());
// 遞歸調(diào)用,顯示子目錄。
listSubDir(f);
}
// 如果是文件,則顯示文件名,不包含路徑信息。
else {
System.out.println(f.getName());
}
}
} catch (IOException e) {
System.err.println("IOException");
}
}
}
package IO;
import java.io.*;
public class FileExample {
public FileExample() {
super();
}
public static void main(String[] args) {
try {
String outfile = "demoout.xml";
String infile = "demoin.xml";
/**
* 用FileOutputStream定義一個(gè)輸入流文件,然后用BuferedOutputStream調(diào)用FileOutputStream對象生成一個(gè)緩沖輸出流
然后用DataOutputStream調(diào)用BuferedOutputStream對象生成數(shù)據(jù)格式化輸出流
*/
DataOutputStream dt=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outfile)));
BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter(dt, "GBK"));
// 對中文的處理
// 定義一個(gè)輸入流
DataInputStream rafFile1 = new DataInputStream(new BufferedInputStream(new FileInputStream(infile)));
// 定義一個(gè)輸入緩沖
BufferedReader rafFile = new BufferedReader(new InputStreamReader(rafFile1, "GBK"));
String xmlcontent = "";
char tag = 0;// 文件友字符0結(jié)束
while (tag != (char) (-1)) {
xmlcontent = xmlcontent + tag + rafFile.readLine() + '\n';
tag = (char) rafFile.read();
}
NewFile.write(xmlcontent);
NewFile.flush();
NewFile.close();
rafFile.close();
System.gc();
} catch (NullPointerException exc) {
exc.printStackTrace();
} catch (java.lang.IndexOutOfBoundsException outb) {
System.out.println(outb.getMessage());
outb.printStackTrace();
} catch (FileNotFoundException fex) {
System.out.println("fex" + fex.getMessage());
} catch (IOException iex) {
System.out.println("iex" + iex.getMessage());
}
}
}
package IO;
import java.io.*;
public class FileRandomRW {
// 需要輸入的person數(shù)目。
public static int NUMBER = 3;
public static void main(String[] args) {
Persons[] people = new Persons[NUMBER];
people[0] = new Persons("張峰", 26, 2000, "N");
people[1] = new Persons("艷娜", 25, 50000, "Y");
people[2] = new Persons("李朋", 50, 7000, "F");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
"peoplerandom.dat"));
// 將人員數(shù)據(jù)保存至“peoplerandom.dat”二進(jìn)制文件中。
writeData(people, out);
// 關(guān)閉流。
out.close();
// 從二進(jìn)制文件“peoplerandom.dat”中逆序讀取數(shù)據(jù)。
RandomAccessFile inOut = new RandomAccessFile("peoplerandom.dat",
"rw");
Persons[] inPeople = readDataReverse(inOut);
// 輸出讀入的數(shù)據(jù)。
System.out.println("原始數(shù)據(jù):");
for (int i = 0; i inPeople.length; i++) {
System.out.println(inPeople[i]);
}
// 修改文件的第三條記錄。
inPeople[2].setSalary(4500);
// 將修改結(jié)果寫入文件。
inPeople[2].writeData(inOut, 3);
// 關(guān)閉流。
inOut.close();
// 從文件中讀入的第三條記錄,并輸出,以驗(yàn)證修改結(jié)果。
RandomAccessFile in = new RandomAccessFile("peoplerandom.dat", "r");
Persons in3People = new Persons();
// 隨機(jī)讀第三條記錄。
in3People.readData(in, 3);
// 關(guān)閉流。
in.close();
System.out.println("修改后的記錄");
System.out.println(in3People);
} catch (IOException exception) {
System.err.println("IOException");
}
}
// 將數(shù)據(jù)寫入輸出流。
static void writeData(Persons[] p, DataOutputStream out) throws IOException {
for (int i = 0; i p.length; i++) {
p[i].writeData(out);
}
}
// 將數(shù)據(jù)從輸入流中逆序讀出。
static Persons[] readDataReverse(RandomAccessFile in) throws IOException {
// 獲得記錄數(shù)目。
int record_num = (int) (in.length() / Persons.RECORD_LENGTH);
Persons[] p = new Persons[record_num];
// 逆序讀取。
for (int i = record_num - 1; i = 0; i--) {
p[i] = new Persons();
// 文件定位。
in.seek(i * Persons.RECORD_LENGTH);
p[i].readData(in, i + 1);
}
return p;
}
}
class Persons {
private String name;
private int age; // 4個(gè)字節(jié)
private double salary; // 8個(gè)字節(jié)
private String married;
public static final int NAME_LENGTH = 20; // 姓名長度
public static final int MARRIED_LENGTH = 2; // 婚否長度
public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8
+ MARRIED_LENGTH * 2;
public Persons() {
}
public Persons(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = "F";
}
public Persons(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
public String setName(String n) {
name = n;
return name;
}
public int setAge(int a) {
age = a;
return age;
}
public double setSalary(double s) {
salary = s;
return salary;
}
public String setMarried(String m) {
married = m;
return married;
}
// 設(shè)置輸出格式。
public String toString() {
return getClass().getName() + "[name=" + name + ",age=" + age
+ ",salary=" + salary + ",married=" + married + "]";
}
// 寫入一條固定長度的記錄,即一個(gè)人的數(shù)據(jù)到輸出流。
public void writeData(DataOutput out) throws IOException {
FixStringIO.writeFixString(name, NAME_LENGTH, out);
out.writeInt(age);
out.writeDouble(salary);
FixStringIO.writeFixString(married, MARRIED_LENGTH, out);
}
// 寫入一條固定長度的記錄到隨機(jī)讀取文件中。
private void writeData(RandomAccessFile out) throws IOException {
FixStringIO.writeFixString(name, NAME_LENGTH, out);
out.writeInt(age);
out.writeDouble(salary);
FixStringIO.writeFixString(married, MARRIED_LENGTH, out);
}
// 隨機(jī)寫入一條固定長度的記錄到輸出流的指定位置。
public void writeData(RandomAccessFile out, int n) throws IOException {
out.seek((n - 1) * RECORD_LENGTH);
writeData(out);
}
// 從輸入流隨機(jī)讀入一條記錄,即一個(gè)人的數(shù)據(jù)。
private void readData(RandomAccessFile in) throws IOException {
name = FixStringIO.readFixString(NAME_LENGTH, in);
age = in.readInt();
salary = in.readDouble();
married = FixStringIO.readFixString(MARRIED_LENGTH, in);
}
// 從輸入流隨機(jī)讀入指定位置的記錄。
public void readData(RandomAccessFile in, int n) throws IOException {
in.seek((n - 1) * RECORD_LENGTH);
readData(in);
}
}
// 對固定長度字符串從文件讀出、寫入文件
class FixStringIO {
// 讀取固定長度的Unicode字符串。
public static String readFixString(int size, DataInput in)
throws IOException {
StringBuffer b = new StringBuffer(size);
int i = 0;
boolean more = true;
while (more i size) {
char ch = in.readChar();
i++;
if (ch == 0) {
more = false;
} else {
b.append(ch);
}
}
// 跳過剩余的字節(jié)。
in.skipBytes(2 * (size - i));
return b.toString();
}
// 寫入固定長度的Unicode字符串。
public static void writeFixString(String s, int size, DataOutput out)
throws IOException {
int i;
for (i = 0; i size; i++) {
char ch = 0;
if (i s.length()) {
ch = s.charAt(i);
}
out.writeChar(ch);
}
}
}
package IO;
import java.io.*;
import java.util.*;
public class FileRW {
// 需要輸入的person數(shù)目。
public static int NUMBER = 3;
public static void main(String[] args) {
Person[] people = new Person[NUMBER];
// 暫時(shí)容納輸入數(shù)據(jù)的臨時(shí)字符串?dāng)?shù)組。
String[] field = new String[4];
// 初始化field數(shù)組。
for (int i = 0; i 4; i++) {
field[i] = "";
}
// IO操作必須捕獲IO異常。
try {
// 用于對field數(shù)組進(jìn)行增加控制。
int fieldcount = 0;
// 先使用System.in構(gòu)造InputStreamReader,再構(gòu)造BufferedReader。
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
for (int i = 0; i NUMBER; i++) {
fieldcount = 0;
System.out.println("The number " + (i + 1) + " person");
System.out
.println("Enter name,age,salary,married(optional),please separate fields by ':'");
// 讀取一行。
String personstr = stdin.readLine();
// 設(shè)置分隔符。
StringTokenizer st = new StringTokenizer(personstr, ":");
// 判斷是否還有分隔符可用。
while (st.hasMoreTokens()) {
field[fieldcount] = st.nextToken();
fieldcount++;
}
// 如果輸入married,則field[3]不為空,調(diào)用具有四個(gè)參數(shù)的Person構(gòu)造函數(shù)。
if (field[3] != "") {
people[i] = new Person(field[0],
Integer.parseInt(field[1]), Double
.parseDouble(field[2]), field[3]);
// 置field[3]為空,以備下次輸入使用。
field[3] = "";
}
// 如果未輸入married,則field[3]為空,調(diào)用具有三個(gè)參數(shù)的Person構(gòu)造函數(shù)。
else {
people[i] = new Person(field[0],
Integer.parseInt(field[1]), Double
.parseDouble(field[2]));
}
}
// 將輸入的數(shù)據(jù)保存至“people.dat”文本文件中。
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter("people.dat")));
writeData(people, out);
// 關(guān)閉流。
out.close();
// 從文件“people.dat”讀取數(shù)據(jù)。
BufferedReader in = new BufferedReader(new FileReader("people.dat"));
Person[] inPeople = readData(in);
// 關(guān)閉流。
in.close();
// 輸出從文件中讀入的數(shù)據(jù)。
for (int i = 0; i inPeople.length; i++) {
System.out.println(inPeople[i]);
}
} catch (IOException exception) {
System.err.println("IOException");
}
}
// 將所有數(shù)據(jù)寫入輸出流。
static void writeData(Person[] p, PrintWriter out) throws IOException {
// 寫入記錄條數(shù),即人數(shù)。
out.println(p.length);
for (int i = 0; i p.length; i++) {
p[i].writeData(out);
}
}
// 將所有數(shù)據(jù)從輸入流中讀出。
static Person[] readData(BufferedReader in) throws IOException {
// 獲取記錄條數(shù),即人數(shù)。
int n = Integer.parseInt(in.readLine());
Person[] p = new Person[n];
for (int i = 0; i n; i++) {
p[i] = new Person();
p[i].readData(in);
}
return p;
}
}
class Person {
private String name;
private int age;
private double salary;
private String married;
public Person() {
}
public Person(String n, int a, double s) {
name = n;
age = a;
salary = s;
married = "F";
}
public Person(String n, int a, double s, String m) {
name = n;
age = a;
salary = s;
married = m;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String getMarried() {
return married;
}
// 設(shè)置輸出格式。
public String toString() {
return getClass().getName() + "[name=" + name + ",age=" + age
+ ",salary=" + salary + ",married=" + married + "]";
}
// 寫入一條記錄,即一個(gè)人的數(shù)據(jù)到輸出流。
public void writeData(PrintWriter out) throws IOException {
// 格式化輸出。
out.println(name + ":" + age + ":" + salary + ":" + married);
}
// 從輸入流讀入一條記錄,即一個(gè)人的數(shù)據(jù)。
public void readData(BufferedReader in) throws IOException {
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, ":");
name = t.nextToken();
age = Integer.parseInt(t.nextToken());
salary = Double.parseDouble(t.nextToken());
married = t.nextToken();
}
}
package IO;
import java.io.*;
public class IOStreamExample {
public static void main(String[] args) throws IOException {
// 1. 讀入一行數(shù)據(jù):
BufferedReader in = new BufferedReader(new FileReader(
"FileStdRead.java"));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "\n";
}
in.close();
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
System.out.print("Enter a line:");
System.out.println(stdin.readLine());
// 2. 從內(nèi)存中讀入
StringReader in2 = new StringReader(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.print((char) c);
}
// 3. 格式化內(nèi)存輸入
try {
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(
s2.getBytes()));
while (true) {
System.out.print((char) in3.readByte());
}
} catch (EOFException e) {
System.err.println("End of stream");
}
// 4. 文件輸入
try {
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(
new FileWriter("IODemo.out")));
int lineCount = 1;
while ((s = in4.readLine()) != null) {
out1.println(lineCount++ + ": " + s);
}
out1.close();
} catch (EOFException e) {
System.err.println("End of stream");
}
// 5. 接收和保存數(shù)據(jù)
try {
DataOutputStream out2 = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeUTF("That was pi");
out2.writeDouble(1.41413);
out2.writeUTF("Square root of 2");
out2.close();
DataInputStream in5 = new DataInputStream(new BufferedInputStream(
new FileInputStream("Data.txt")));
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
} catch (EOFException e) {
throw new RuntimeException(e);
}
// 6. 隨機(jī)讀取文件內(nèi)容
RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
for (int i = 0; i 10; i++) {
rf.writeDouble(i * 1.414);
}
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
rf.seek(5 * 8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for (int i = 0; i 10; i++) {
System.out.println("Value " + i + ": " + rf.readDouble());
}
rf.close();
}
}
package IO;
import java.io.*;
/**
* p
* Title: JAVA進(jìn)階訣竅
* /p
*
* @author 張峰
* @version 1.0
*/
public class MakeDirectoriesExample {
private static void fileattrib(File f) {
System.out.println("絕對路徑: " + f.getAbsolutePath() + "\n 可讀屬性: "
+ f.canRead() + "\n 可定屬性: " + f.canWrite() + "\n 文件名: "
+ f.getName() + "\n 父目錄: " + f.getParent() + "\n 當(dāng)前路徑: "
+ f.getPath() + "\n 文件長度: " + f.length() + "\n 最后更新日期: "
+ f.lastModified());
if (f.isFile()) {
System.out.println("輸入的是一個(gè)文件");
} else if (f.isDirectory()) {
System.out.println("輸入的是一個(gè)目錄");
}
}
public static void main(String[] args) {
if (args.length 1) {
args = new String[3];
}
args[0] = "d";
args[1] = "test1.txt";
args[2] = "test2.txt";
File old = new File(args[1]), rname = new File(args[2]);
old.renameTo(rname);
fileattrib(old);
fileattrib(rname);
int count = 0;
boolean del = false;
if (args[0].equals("d")) {
count++;
del = true;
}
count--;
while (++count args.length) {
File f = new File(args[count]);
if (f.exists()) {
System.out.println(f + " 文件己經(jīng)存在");
if (del) {
System.out.println("刪除文件" + f);
f.delete();
}
} else { // 如果文件不存在
if (!del) {
f.mkdirs();
System.out.println("創(chuàng)建文件: " + f);
}
}
fileattrib(f);
}
}
}
最簡單的java代碼肯定就是這個(gè)了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來我們的java實(shí)驗(yàn)班試聽,有免費(fèi)的試聽課程幫助學(xué)習(xí)java必備基礎(chǔ)知識,有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評團(tuán)進(jìn)行專業(yè)測試,幫助測評學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門java,更好的學(xué)習(xí)java!
網(wǎng)頁名稱:經(jīng)典java代碼,java代碼大全
URL地址:http://chinadenli.net/article19/dsigddh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、企業(yè)建站、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)