Rays的學習記錄網誌
版主:Rays
專長:JAVA,PHP,C#,VC++,NetWork,資訊安全,資料庫
證照:MCSE,MCSA,MCDBA,CCNA,LPI,CISSP
2013年8月1日 星期四
2011年11月8日 星期二
2011年7月8日 星期五
2011-07-08 電腦概論
Quagga Routing Suite (動態路由)
vm1
eth0 : LAN1 192.168.X.6/29
eth1 : LAN2 192.168.X.14/29
eth2 : LAN3 192.168.X.25/29
vm2
eth0 : LAN4 192.168.X.22/29
eth1 : LAN3 192.168.X.26/29
eth2 : LAN5 192.168.X.41/29
vm3
eth0 : LAN6 192.168.X.38/29
eth1 : LAN5 192.168.X.42/29
eth2 : 橋接介面卡 192.168.51.X/24
vm3
$su -
安裝路由協定的套件
#yum install quagga -y
#setup 關閉防火牆
初始化所有網路卡NIC
#ifconfig eth0 0.0.0.0 up
#ifconfig eth1 0.0.0.0 up
#ifconfig eth2 0.0.0.0 up
複製設定檔
#cd /etc/quagga
查看該目錄下的檔案
#ls
#cp ripd.conf.sample ripd.conf
#cp ospfd.conf.sample ospfd.conf
啟動zebra & rip 的服務
切換到 /etc/init.d 目錄
#cd /etc/init.d
查看一下目錄下的檔案, 此檔案下的所有檔案都是可執行的程式 (服務)
#ls
zebra 是負責 ip 層設定的服務
#/etc/init.d/zebra start
ripd 是負責 動態路由 RIP 協動的服務
#/etc/init.d/ripd start
正式進入 quagga 的整合模式, exit 回上一層, Ctrl+Z 直接回到第一層
#vtysh
進入 模組 模式
localhost.localdomain#config t
(config)#hostname vm3
進入網路介面卡 eth0 設定 IP 位址並啟動
vm3(config)#interface eth0
vm3(config-if)#ip address 192.168.X.38/29
vm3(config-if)#no shutdown
vm3(config-if)#exit
vm3(config)#interface eth1
vm3(config-if)#ip address 192.168.X.42/29
vm3(config-if)#no shutdown
vm3(config-if)#exit
vm3(config)#interface eth2
vm3(config-if)#ip address 192.168.51.X/24
vm3(config-if)#no shutdown
vm3(config-if)#exit
啟動 forwarding 的功能
vm3(config)#ip forwarding
vm3(config)#exit
查看剛剛所設定的資訊(目前的狀態)
vm3#show running-config?
查看開機時設定的資訊(就是看 zebra.conf & ripd.conf 的內容)
vm3#show startup-config
查看路由表
vm3#show ip route
啟動 RIPv2 的路由協定
vm3#config t
vm3(config)#router rip
vm3(config-router)#version 2
vm3(config-router)#network 192.168.51.0/24
vm3(config-router)#network 192.168.X.32/29
vm3(config-router)#network 192.168.X.40/29
vm3(config-router)#Ctrl+Z
vm3#show ip route
vm1
eth0 : LAN1 192.168.X.6/29
eth1 : LAN2 192.168.X.14/29
eth2 : LAN3 192.168.X.25/29
vm2
eth0 : LAN4 192.168.X.22/29
eth1 : LAN3 192.168.X.26/29
eth2 : LAN5 192.168.X.41/29
vm3
eth0 : LAN6 192.168.X.38/29
eth1 : LAN5 192.168.X.42/29
eth2 : 橋接介面卡 192.168.51.X/24
vm3
$su -
安裝路由協定的套件
#yum install quagga -y
#setup 關閉防火牆
初始化所有網路卡NIC
#ifconfig eth0 0.0.0.0 up
#ifconfig eth1 0.0.0.0 up
#ifconfig eth2 0.0.0.0 up
複製設定檔
#cd /etc/quagga
查看該目錄下的檔案
#ls
#cp ripd.conf.sample ripd.conf
#cp ospfd.conf.sample ospfd.conf
啟動zebra & rip 的服務
切換到 /etc/init.d 目錄
#cd /etc/init.d
查看一下目錄下的檔案, 此檔案下的所有檔案都是可執行的程式 (服務)
#ls
zebra 是負責 ip 層設定的服務
#/etc/init.d/zebra start
ripd 是負責 動態路由 RIP 協動的服務
#/etc/init.d/ripd start
正式進入 quagga 的整合模式, exit 回上一層, Ctrl+Z 直接回到第一層
#vtysh
進入 模組 模式
localhost.localdomain#config t
(config)#hostname vm3
進入網路介面卡 eth0 設定 IP 位址並啟動
vm3(config)#interface eth0
vm3(config-if)#ip address 192.168.X.38/29
vm3(config-if)#no shutdown
vm3(config-if)#exit
vm3(config)#interface eth1
vm3(config-if)#ip address 192.168.X.42/29
vm3(config-if)#no shutdown
vm3(config-if)#exit
vm3(config)#interface eth2
vm3(config-if)#ip address 192.168.51.X/24
vm3(config-if)#no shutdown
vm3(config-if)#exit
啟動 forwarding 的功能
vm3(config)#ip forwarding
vm3(config)#exit
查看剛剛所設定的資訊(目前的狀態)
vm3#show running-config?
查看開機時設定的資訊(就是看 zebra.conf & ripd.conf 的內容)
vm3#show startup-config
查看路由表
vm3#show ip route
啟動 RIPv2 的路由協定
vm3#config t
vm3(config)#router rip
vm3(config-router)#version 2
vm3(config-router)#network 192.168.51.0/24
vm3(config-router)#network 192.168.X.32/29
vm3(config-router)#network 192.168.X.40/29
vm3(config-router)#Ctrl+Z
vm3#show ip route
2011年7月7日 星期四
2011-07-07 JAVA
http://tw.zkoss.org/ Firamework
1.http://tw.zkoss.org Firamework
// 學習月份的英文單字的程式
import java.util.Random;
import java.util.Scanner;
class MonthCAI {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
String[] monthString = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
}; //另一空間起始記憶體位址
int month = rand.nextInt(12); // 對應的月份:0~11的亂數
System.out.println("問題為" + monthString[month]);
int c=0;
while (true) {
System.out.print("這是幾月呢:");
int m = stdIn.nextInt();
if (m == month + 1)
break;
else {
++c;
System.out.println("答錯了。");
}
if (c>=2)
System.exit(1); //1退出
}
System.out.println("正確答案。");
}
}
-----------------------------------------------------------------------
// 顯示陣列變數的值
class PrintArray {
public static void main(String[] args) {
int[] a = new int[5];
System.out.println("a = " + a);
a = null; //清除記憶體位址
System.out.println("a = " + a);
}
}
-------------------------------------------------------------
// 合計2列3行的矩陣
class Matrix_test {
public static void main(String[] args) {
int[][] x = { {0, 2, 0}, {1, 0, 1} ,{1,1,1}};
for (int i = 0; i < x.length; ++i) {
for (int j = 0; j < x[i].length ; ++j)
//c[i][j] = a[i][j] + b[i][j];
System.out.println(x[i][j]==1 ? '*' : ' ');
}
}
}
-------------------------------------------------------------
//網路抓檔
import java.net.*;
import java.io.*;
public class GetURL1 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.71/abc.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); //建立連線;讀進檔案
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line+"\n");
}
in.close();
System.out.println(sb.toString());
}
}
----------------------------------------------------------------------------
import java.net.*;
import java.io.*;
public class GetURL3 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.50/abc.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
int v=0;
String line;
while ((line = in.readLine()) != null) {
for (int i=0; i < line.length() ; i++) {
char c = line.charAt(i); //字串比較
if ( c >=48 && c<=57 )
//System.out.print(c);
v++;
}
}System.out.println(v);
in.close();
//System.out.println(c);
}
}
---------------------------------------------------------------
import java.net.*;
import java.io.*;
public class GetURL4 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.50/abc1.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
String[] a1 = line.split(":");
System.out.println(a1[1]);
}
in.close();
//System.out.println(c);
}
}
--------------------------------------------------------------
import java.net.*;
import java.io.*;
public class GetURL5 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.50/abc2.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
//String s ;
//String[] a1 = s.split(":");
//System.out.println(a1[1]);
int sum=0;
String s;
while ((s = in.readLine()) != null) {
if (s.isEmpty() == false) {
String[] a1 = s.split(":");
int t=Integer.parseInt(a1[1]);
sum+= t ;
}
} System.out.print(sum);
in.close();
//System.out.println(c);
}
}
----------------------------------------------------------------
String x = "123"; //必需為數字,否則不執行parseInt()
int y = Integer parseInt(x); //字串轉型數值int
-----------------------------------------------------------------
White Space
ASC 10,13 (換行)
ASC 9 (TAB)
ASC 32 (空白)
1.http://tw.zkoss.org Firamework
// 學習月份的英文單字的程式
import java.util.Random;
import java.util.Scanner;
class MonthCAI {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
String[] monthString = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
}; //另一空間起始記憶體位址
int month = rand.nextInt(12); // 對應的月份:0~11的亂數
System.out.println("問題為" + monthString[month]);
int c=0;
while (true) {
System.out.print("這是幾月呢:");
int m = stdIn.nextInt();
if (m == month + 1)
break;
else {
++c;
System.out.println("答錯了。");
}
if (c>=2)
System.exit(1); //1退出
}
System.out.println("正確答案。");
}
}
-----------------------------------------------------------------------
// 顯示陣列變數的值
class PrintArray {
public static void main(String[] args) {
int[] a = new int[5];
System.out.println("a = " + a);
a = null; //清除記憶體位址
System.out.println("a = " + a);
}
}
-------------------------------------------------------------
// 合計2列3行的矩陣
class Matrix_test {
public static void main(String[] args) {
int[][] x = { {0, 2, 0}, {1, 0, 1} ,{1,1,1}};
for (int i = 0; i < x.length; ++i) {
for (int j = 0; j < x[i].length ; ++j)
//c[i][j] = a[i][j] + b[i][j];
System.out.println(x[i][j]==1 ? '*' : ' ');
}
}
}
-------------------------------------------------------------
//網路抓檔
import java.net.*;
import java.io.*;
public class GetURL1 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.71/abc.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); //建立連線;讀進檔案
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line+"\n");
}
in.close();
System.out.println(sb.toString());
}
}
----------------------------------------------------------------------------
import java.net.*;
import java.io.*;
public class GetURL3 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.50/abc.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
int v=0;
String line;
while ((line = in.readLine()) != null) {
for (int i=0; i < line.length() ; i++) {
char c = line.charAt(i); //字串比較
if ( c >=48 && c<=57 )
//System.out.print(c);
v++;
}
}System.out.println(v);
in.close();
//System.out.println(c);
}
}
---------------------------------------------------------------
import java.net.*;
import java.io.*;
public class GetURL4 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.50/abc1.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
String[] a1 = line.split(":");
System.out.println(a1[1]);
}
in.close();
//System.out.println(c);
}
}
--------------------------------------------------------------
import java.net.*;
import java.io.*;
public class GetURL5 {
public static void main(String argv[]) throws Exception {
URL url = new URL("http://140.137.222.50/abc2.txt"); //URL.class
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
//String s ;
//String[] a1 = s.split(":");
//System.out.println(a1[1]);
int sum=0;
String s;
while ((s = in.readLine()) != null) {
if (s.isEmpty() == false) {
String[] a1 = s.split(":");
int t=Integer.parseInt(a1[1]);
sum+= t ;
}
} System.out.print(sum);
in.close();
//System.out.println(c);
}
}
----------------------------------------------------------------
String x = "123"; //必需為數字,否則不執行parseInt()
int y = Integer parseInt(x); //字串轉型數值int
-----------------------------------------------------------------
White Space
ASC 10,13 (換行)
ASC 9 (TAB)
ASC 32 (空白)
2011年7月5日 星期二
2011-07-05 電腦概論
狀況一
vm1 網卡 : LAN1
vm2 網卡 : LAN1
vm3 網卡 : LAN1
vm1的設定
$su -
#ifconfig eth0 192.168.1.1/24 up
vm2的設定
$su -
#ifconfig eth0 192.168.2.2/24 up
此時vm1 與 vm2 應該互 ping 會不通
[原因]
雖然實體是連接在一起, 不過分屬於兩個不同的網段 192.168.1.0/24 與 192.168.2.0/24
啟動 vm3並設定 IP 位址
$su -
#setup 關閉防火牆
#ifconfig eth0 192.168.1.254/24 up
#ifconfig eth0:1 192.168.2.254/24 up
將vm1與vm2設定加入 預設閘道
vm1
#ip route add default via 192.168.1.254
vm2
#ip route add default via 192.168.2.254
啟動vm3的 forwarding 的功能
cat /proc/sys/net/ipv4/ip_forward
#echo "1" > /proc/sys/net/ipv4/ip_forward
此時的vm1 與 vm2 應該可以互通嚕.........YA
狀況二
vm1 網卡 : LAN1
vm2 網卡 : LAN2
vm3 網卡 : LAN1 , LAN2
vm1的設定
$su -
#ifconfig eth0 192.168.1.1/24 up
#ip route add default via 192.168.1.254
vm2的設定
$su -
#ifconfig eth0 192.168.2.2/24 up
#ip route add default via 192.168.2.254
啟動 vm3並設定 IP 位址
$su -
#setup 關閉防火牆
#ifconfig eth0 192.168.1.254/24 up
#ifconfig eth1 192.168.2.254/24 up
啟動vm3的 forwarding 的功能
#echo "1" > /proc/sys/net/ipv4/ip_forward
此時的vm1 與 vm2 應該可以互通嚕.........YA
vm1 網卡 : LAN1
vm2 網卡 : LAN1
vm3 網卡 : LAN1
vm1的設定
$su -
#ifconfig eth0 192.168.1.1/24 up
vm2的設定
$su -
#ifconfig eth0 192.168.2.2/24 up
此時vm1 與 vm2 應該互 ping 會不通
[原因]
雖然實體是連接在一起, 不過分屬於兩個不同的網段 192.168.1.0/24 與 192.168.2.0/24
啟動 vm3並設定 IP 位址
$su -
#setup 關閉防火牆
#ifconfig eth0 192.168.1.254/24 up
#ifconfig eth0:1 192.168.2.254/24 up
將vm1與vm2設定加入 預設閘道
vm1
#ip route add default via 192.168.1.254
vm2
#ip route add default via 192.168.2.254
啟動vm3的 forwarding 的功能
cat /proc/sys/net/ipv4/ip_forward
#echo "1" > /proc/sys/net/ipv4/ip_forward
此時的vm1 與 vm2 應該可以互通嚕.........YA
狀況二
vm1 網卡 : LAN1
vm2 網卡 : LAN2
vm3 網卡 : LAN1 , LAN2
vm1的設定
$su -
#ifconfig eth0 192.168.1.1/24 up
#ip route add default via 192.168.1.254
vm2的設定
$su -
#ifconfig eth0 192.168.2.2/24 up
#ip route add default via 192.168.2.254
啟動 vm3並設定 IP 位址
$su -
#setup 關閉防火牆
#ifconfig eth0 192.168.1.254/24 up
#ifconfig eth1 192.168.2.254/24 up
啟動vm3的 forwarding 的功能
#echo "1" > /proc/sys/net/ipv4/ip_forward
此時的vm1 與 vm2 應該可以互通嚕.........YA
2011年7月4日 星期一
2011-07-04 JAVA
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class LinearSearch {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
for (int j = 0; j < n; j++)
System.out.print(a[j] + " ");
System.out.println(" }");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = 0; i < n; i++)
if (a[i] == key)
break;
if (i < n) // 搜尋成功
System.out.println("該值存在於a[" + i + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
----------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class LinearSearch {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
//for (int j = 0; j < n; j++)
//System.out.print(a[j] + " ");
//System.out.println(" }");
for (int j : a)
System.out.print(j + " ");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = 0; i < n; i++)
if (a[i] == key)
break;
if (i < n) // 搜尋成功
System.out.println("該值存在於a[" + i + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
--------------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class MyP6_7 {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
//for (int j = 0; j < n; j++)
//System.out.print(a[j] + " ");
//System.out.println(" }");
for (int j : a)
System.out.print(j + " ");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = n-1; i >= 0; i--) //倒回來找
if (a[i] == key)
break;
if (i < n) // 搜尋成功
System.out.println("該值存在於a[" + (i+1) + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
-----------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class MyP6_7 {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
//for (int j = 0; j < n; j++)
//System.out.print(a[j] + " ");
//System.out.println(" }");
for (int j : a)
System.out.print(j + " ");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = n-1; i >= 0; i--) //倒回來找
if (a[i] == key)
break;
if (i < n && i!=-1) // 搜尋成功
System.out.println("該值存在於a[" + (i+1) + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
----------------------------------------------------------------------------------------
// 將陣列元素的順序逆向重整並顯示
import java.util.Random;
import java.util.Scanner;
class ReverseArray {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
System.out.print("元素數:");
int n = stdIn.nextInt(); // 輸入元素數
int[] a = new int[n]; // 宣告陣列
for (int i = 0; i < n; i++) {
a[i] = 10 + rand.nextInt(90);
System.out.println("a[" + i + "] = " + a[i]);
}
for (int i = 0; i < n / 2; i++) {
int t = a[i];
a[i] = a[n - i - 1]; //a[i]與a[n-i-1] swap交換
a[n - i - 1] = t;
}
System.out.println("將元素的排列順序逆轉。");
for (int i = 0; i < n; i++)
System.out.println("a[" + i + "] = " + a[i]);
}
}
----------------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class MyP6_12 {
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,7,8,9};
for(int i=0 ; i<10 ; ++i){ //i<10,跑幾次,不一定是10
int r = rand.nextInt(9); //0~8
int t;
t=a[r];
a[r]=a[r+1];
a[r+1]=t;
}
for (int v :a)
System.out.print(v+",");
}
}
---------------------------------------------------------------------------------------------
/**
* 處理檔案及目錄
*
* @author (Shieh-Fu Chen)
* @version (V 1.0 2011/06/27)
*
* 執行結果
* ======================================================================
* CD=D:\bobe\toBalaTech\Develop\Java\java2009\tools\npp587
* D:\\fred.txt
*/
import java.util.Scanner;
import java.io.*;
public class MyFile2 {
public static void main (String args[]) throws IOException {
System.out.println("user.dir = " + System.getProperty("user.dir")); // JVM環境變數;取得 user.dir 環境變數的值
System.setProperty("user.dir", "D:/"); // 改變 user.dir 環境變數的值
// java.io.File 這類別主要是處理檔案及目錄的 PathName, 而不是處理檔案的 I/O (開檔, 讀檔, 存檔),
// 下式只是取得 fred.txt 完整目錄名稱, 並沒有建立新檔
String s = new File("fred.txt").getAbsolutePath(); //File class;檔案不存在一樣成立
System.out.println(s);
if (new File(s).exists()) {
Scanner stdIn = new Scanner(System.in);
System.out.println("進行倒數: ");
int x;
System.out.print("正整數值: ");
x = stdIn.nextInt();
if (x == 0)
System.out.println(new File(s).delete());
}
System.out.println("檔案不存在");
}
}
-------------------------------------------------------------------------------------
/**
* 建立文字檔, 並寫入字串資料
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*/
import java.util.Scanner;
import java.io.*;
class MyWriter1 {
public static void main(String[] argv) throws IOException {
// PrintWriter 沒有提供 Append File 的建構子
PrintWriter pWriter = new PrintWriter("d:\\MyWriter1.txt"); //PrintWriter產生一檔案,如有`存在檔案則將內容清空
Scanner stdIn = new Scanner(System.in);
System.out.print("ID:");
String s = stdIn.nextLine(); //nextLine輸入字串,空白亦可
System.out.print("Score:");
int a = stdIn.nextInt();
// 寫入檔案
pWriter.print(s+",");
pWriter.print(a);
// 關閉檔案
pWriter.close();
}
}
---------------------------------------------------------------------------------------
/**
* 將輸入成績, 附加在檔案後面
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*
*/
import java.util.Scanner;
import java.io.*;
class MyWriter2 {
public static void main(String[] argv) throws IOException{
// FileWriter 有提供 Append File 的建構子, 但是寫入的方法 (write()) 只提供字元及字串資料型態
FileWriter fWriter = new FileWriter("d:\\MyWriter2.txt", true); //FileWriter-->true資料附加
Scanner stdIn = new Scanner(System.in);
System.out.print("Name:");
String n = stdIn.nextLine();
System.out.print("Address:");
String a = stdIn.nextLine();
fWriter.write(n+","); // write() 無法寫入 double 資料
fWriter.write(a);
fWriter.write("\n"); // 在 記事本 中, 並沒有換行, 應如何解決 ? 在檔案是以ASC碼的10,13(微軟),其它OS為10
fWriter.close();
}
}
-----------------------------------------------------------------------------------------------------
/**
* 輸入五位同學, 國英數三科成績,存入文字檔
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*
*/
import java.util.Scanner;
import java.io.*;
class MyWriter3 {
public static void main(String[] argv) throws IOException {
// FileWriter 有提供 Append File 的建構子
FileWriter fWriter = new FileWriter("d:\\MyWriter3.txt", true);
// PrintWriter 有提供好用的寫入方法 print()
PrintWriter pWriter = new PrintWriter(fWriter);
Scanner stdIn = new Scanner(System.in);
System.out.print("學號:");
String n = stdIn.nextLine();
System.out.print("國文:");
int c = stdIn.nextInt();
System.out.print("英文:");
int e = stdIn.nextInt();
System.out.print("數學:");
int m = stdIn.nextInt();
pWriter.print(n+","); // 寫入字串
pWriter.print(c+","); // 寫入整數
pWriter.print(e+","); // 寫入整數
pWriter.print(m); // 寫入整數
// 根據不同作業系統, 寫入正確的換行碼
pWriter.print(System.getProperty("line.separator")); //取得JVM環境變數
pWriter.close();
}
}
----------------------------------------------------------------------------------------------------------
/**
* 讀出文字檔內容
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*/
import java.io.*;
public class MyReader1 {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("d:\\MyWriter2.txt"); //FileReader讀入檔案;如檔案不存在, 則無法RUN;
BufferedReader bReader = new BufferedReader(fileReader); //緩衝;提供很好用的METHOD readLine()
String s;
while((s = bReader.readLine()) != null ) {
System.out.println(s);
}
bReader.close();
}
}
--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.io.*;
class MyReaderWriter {
public static void main(String[] args)throws IOException{
FileWriter fWriter = new FileWriter("d:\\out.txt", false);
FileReader fileReader = new FileReader("d:\\MyWriter2.txt");
BufferedReader bReader = new BufferedReader(fileReader);
String s;
while((s = bReader.readLine()) != null ) {
for (int i=0; i < s.length() ; i++) {
char c = s.charAt(i);
fWriter.write(c==',' ? '$' : c);
/* /* if ( c == ',' )
System.out.print('$');
else
System.out.print(c); */
}
fWriter.write(System.getProperty("line.separator"));
}
fileReader.close();
fWriter.close();
}
}
---------------------------------------------------------------------------------------
/**
* 讀出文字檔內容, 並解析單行文字
*
* @author (Shieh-Fu Chen)
* @version (V 0.1 2011/07/04)
*
*/
import java.io.*;
public class dafumenu {
public static void main(String[] args) throws IOException{
FileReader fileReader = new FileReader("d:\\dafumenu.txt");
BufferedReader bReader = new BufferedReader(fileReader);
String s;
while((s = bReader.readLine()) != null ) {
if (s.equals("[system]"))
System.out.println("system");
else if (s.equals("[account]"))
System.out.println("account");
else if (s.equals("[student]"))
System.out.println("student");
else
System.out.println();
}
fileReader.close();
}
//fileReader.close();
}
import java.util.Random;
import java.util.Scanner;
class LinearSearch {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
for (int j = 0; j < n; j++)
System.out.print(a[j] + " ");
System.out.println(" }");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = 0; i < n; i++)
if (a[i] == key)
break;
if (i < n) // 搜尋成功
System.out.println("該值存在於a[" + i + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
----------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class LinearSearch {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
//for (int j = 0; j < n; j++)
//System.out.print(a[j] + " ");
//System.out.println(" }");
for (int j : a)
System.out.print(j + " ");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = 0; i < n; i++)
if (a[i] == key)
break;
if (i < n) // 搜尋成功
System.out.println("該值存在於a[" + i + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
--------------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class MyP6_7 {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
//for (int j = 0; j < n; j++)
//System.out.print(a[j] + " ");
//System.out.println(" }");
for (int j : a)
System.out.print(j + " ");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = n-1; i >= 0; i--) //倒回來找
if (a[i] == key)
break;
if (i < n) // 搜尋成功
System.out.println("該值存在於a[" + (i+1) + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
-----------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class MyP6_7 {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
final int n = 12; // 元素數
int[] a = new int[n]; // 宣告陣列
for (int j = 0; j < n; j++)
a[j] = rand.nextInt(10);
System.out.print("陣列a的所有元素的值\n{ ");
//for (int j = 0; j < n; j++)
//System.out.print(a[j] + " ");
//System.out.println(" }");
for (int j : a)
System.out.print(j + " ");
System.out.print("搜尋的數值:");
int key = stdIn.nextInt();
int i;
for (i = n-1; i >= 0; i--) //倒回來找
if (a[i] == key)
break;
if (i < n && i!=-1) // 搜尋成功
System.out.println("該值存在於a[" + (i+1) + "]。");
else // 搜尋失敗
System.out.println("該值不存在。");
}
}
----------------------------------------------------------------------------------------
// 將陣列元素的順序逆向重整並顯示
import java.util.Random;
import java.util.Scanner;
class ReverseArray {
public static void main(String[] args) {
Random rand = new Random();
Scanner stdIn = new Scanner(System.in);
System.out.print("元素數:");
int n = stdIn.nextInt(); // 輸入元素數
int[] a = new int[n]; // 宣告陣列
for (int i = 0; i < n; i++) {
a[i] = 10 + rand.nextInt(90);
System.out.println("a[" + i + "] = " + a[i]);
}
for (int i = 0; i < n / 2; i++) {
int t = a[i];
a[i] = a[n - i - 1]; //a[i]與a[n-i-1] swap交換
a[n - i - 1] = t;
}
System.out.println("將元素的排列順序逆轉。");
for (int i = 0; i < n; i++)
System.out.println("a[" + i + "] = " + a[i]);
}
}
----------------------------------------------------------------------------------------
// 線性搜尋
import java.util.Random;
import java.util.Scanner;
class MyP6_12 {
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,7,8,9};
for(int i=0 ; i<10 ; ++i){ //i<10,跑幾次,不一定是10
int r = rand.nextInt(9); //0~8
int t;
t=a[r];
a[r]=a[r+1];
a[r+1]=t;
}
for (int v :a)
System.out.print(v+",");
}
}
---------------------------------------------------------------------------------------------
/**
* 處理檔案及目錄
*
* @author (Shieh-Fu Chen)
* @version (V 1.0 2011/06/27)
*
* 執行結果
* ======================================================================
* CD=D:\bobe\toBalaTech\Develop\Java\java2009\tools\npp587
* D:\\fred.txt
*/
import java.util.Scanner;
import java.io.*;
public class MyFile2 {
public static void main (String args[]) throws IOException {
System.out.println("user.dir = " + System.getProperty("user.dir")); // JVM環境變數;取得 user.dir 環境變數的值
System.setProperty("user.dir", "D:/"); // 改變 user.dir 環境變數的值
// java.io.File 這類別主要是處理檔案及目錄的 PathName, 而不是處理檔案的 I/O (開檔, 讀檔, 存檔),
// 下式只是取得 fred.txt 完整目錄名稱, 並沒有建立新檔
String s = new File("fred.txt").getAbsolutePath(); //File class;檔案不存在一樣成立
System.out.println(s);
if (new File(s).exists()) {
Scanner stdIn = new Scanner(System.in);
System.out.println("進行倒數: ");
int x;
System.out.print("正整數值: ");
x = stdIn.nextInt();
if (x == 0)
System.out.println(new File(s).delete());
}
System.out.println("檔案不存在");
}
}
-------------------------------------------------------------------------------------
/**
* 建立文字檔, 並寫入字串資料
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*/
import java.util.Scanner;
import java.io.*;
class MyWriter1 {
public static void main(String[] argv) throws IOException {
// PrintWriter 沒有提供 Append File 的建構子
PrintWriter pWriter = new PrintWriter("d:\\MyWriter1.txt"); //PrintWriter產生一檔案,如有`存在檔案則將內容清空
Scanner stdIn = new Scanner(System.in);
System.out.print("ID:");
String s = stdIn.nextLine(); //nextLine輸入字串,空白亦可
System.out.print("Score:");
int a = stdIn.nextInt();
// 寫入檔案
pWriter.print(s+",");
pWriter.print(a);
// 關閉檔案
pWriter.close();
}
}
---------------------------------------------------------------------------------------
/**
* 將輸入成績, 附加在檔案後面
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*
*/
import java.util.Scanner;
import java.io.*;
class MyWriter2 {
public static void main(String[] argv) throws IOException{
// FileWriter 有提供 Append File 的建構子, 但是寫入的方法 (write()) 只提供字元及字串資料型態
FileWriter fWriter = new FileWriter("d:\\MyWriter2.txt", true); //FileWriter-->true資料附加
Scanner stdIn = new Scanner(System.in);
System.out.print("Name:");
String n = stdIn.nextLine();
System.out.print("Address:");
String a = stdIn.nextLine();
fWriter.write(n+","); // write() 無法寫入 double 資料
fWriter.write(a);
fWriter.write("\n"); // 在 記事本 中, 並沒有換行, 應如何解決 ? 在檔案是以ASC碼的10,13(微軟),其它OS為10
fWriter.close();
}
}
-----------------------------------------------------------------------------------------------------
/**
* 輸入五位同學, 國英數三科成績,存入文字檔
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*
*/
import java.util.Scanner;
import java.io.*;
class MyWriter3 {
public static void main(String[] argv) throws IOException {
// FileWriter 有提供 Append File 的建構子
FileWriter fWriter = new FileWriter("d:\\MyWriter3.txt", true);
// PrintWriter 有提供好用的寫入方法 print()
PrintWriter pWriter = new PrintWriter(fWriter);
Scanner stdIn = new Scanner(System.in);
System.out.print("學號:");
String n = stdIn.nextLine();
System.out.print("國文:");
int c = stdIn.nextInt();
System.out.print("英文:");
int e = stdIn.nextInt();
System.out.print("數學:");
int m = stdIn.nextInt();
pWriter.print(n+","); // 寫入字串
pWriter.print(c+","); // 寫入整數
pWriter.print(e+","); // 寫入整數
pWriter.print(m); // 寫入整數
// 根據不同作業系統, 寫入正確的換行碼
pWriter.print(System.getProperty("line.separator")); //取得JVM環境變數
pWriter.close();
}
}
----------------------------------------------------------------------------------------------------------
/**
* 讀出文字檔內容
*
* @author (Sung-Lin Chen)
* @version (V 0.1 2011/04/22)
*/
import java.io.*;
public class MyReader1 {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("d:\\MyWriter2.txt"); //FileReader讀入檔案;如檔案不存在, 則無法RUN;
BufferedReader bReader = new BufferedReader(fileReader); //緩衝;提供很好用的METHOD readLine()
String s;
while((s = bReader.readLine()) != null ) {
System.out.println(s);
}
bReader.close();
}
}
--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.io.*;
class MyReaderWriter {
public static void main(String[] args)throws IOException{
FileWriter fWriter = new FileWriter("d:\\out.txt", false);
FileReader fileReader = new FileReader("d:\\MyWriter2.txt");
BufferedReader bReader = new BufferedReader(fileReader);
String s;
while((s = bReader.readLine()) != null ) {
for (int i=0; i < s.length() ; i++) {
char c = s.charAt(i);
fWriter.write(c==',' ? '$' : c);
/* /* if ( c == ',' )
System.out.print('$');
else
System.out.print(c); */
}
fWriter.write(System.getProperty("line.separator"));
}
fileReader.close();
fWriter.close();
}
}
---------------------------------------------------------------------------------------
/**
* 讀出文字檔內容, 並解析單行文字
*
* @author (Shieh-Fu Chen)
* @version (V 0.1 2011/07/04)
*
*/
import java.io.*;
public class dafumenu {
public static void main(String[] args) throws IOException{
FileReader fileReader = new FileReader("d:\\dafumenu.txt");
BufferedReader bReader = new BufferedReader(fileReader);
String s;
while((s = bReader.readLine()) != null ) {
if (s.equals("[system]"))
System.out.println("system");
else if (s.equals("[account]"))
System.out.println("account");
else if (s.equals("[student]"))
System.out.println("student");
else
System.out.println();
}
fileReader.close();
}
//fileReader.close();
}
訂閱:
意見 (Atom)