一段关于JAVA程序升级的问题(Changing bits) 请高手帮忙实现一个IO的Java程序题
\u4e0b\u5217\u6709\u5173Java\u7a0b\u5e8f\u7684\u8bf4\u6cd5\u4e2d\u6b63\u786e\u7684\u662f\u9009D
A \u5373\u4f7fjava\u662f\u89e3\u91ca\u6267\u884c\u7684\u4f46\u662f\u4ecd\u9700\u8981\u8fdb\u884c\u7f16\u8bd1
B\u4e00\u4e2ajava\u6e90\u7a0b\u5e8f\u7f16\u8bd1\u540e\u53ef\u80fd\u4ea7\u751f\u4e00\u4e2a\u6216\u8005\u591a\u4e2aclass\u7684\u5b57\u8282\u7801\u6587\u4ef6
C\u5b89\u88c5JDK\u540e\u4ecd\u9700\u624b\u52a8\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u624d\u53ef\u6b63\u5e38\u4f7f\u7528
\u8c22\u8c22\u91c7\u7eb3
\u4f60\u6240\u8bf4\u7684\u65e5\u5386\u7c7b\u53d6\u5f97\u5929\u6570\u6bcf\u4e2a\u6708\u90fd\u662f31\u5929\uff0c\u6211\u770b\u4e86\uff0c
\u539f\u56e0\u662f\u6211\u63a5\u6536\u7684\u6708\u4efd\u53c2\u6570\u662f0--11\uff0c\u4f60\u8f93\u516511\uff0c\u5b9e\u9645
\u4e0a\u662f\u8868\u793a12\u6708\uff0c\u8f93\u51650\uff0c\u8868\u793a1\u6708\u3002
-----------StuClass.java------------------
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StuClass{
protected StuClass() throws IOException, SecurityException {
super();
}
private static final String FILE_PATH = "D:\\data.dat";
private List stuList = new ArrayList();
/**
* \u4fdd\u5b58\u589e\u52a0\u7684\u5b66\u751f
*
* @param stu
* @throws IOException
*/
public void saveStudent(Student stu) throws IOException {
File f = new File(FILE_PATH);
OutputStream output = new FileOutputStream(f, true);
MyObjectWriter objOut = new MyObjectWriter(output);
objOut.writeObject(stu);
objOut.close();
output.close();
}
/**
* \u589e\u52a0\u5b66\u751f
*
* @param student
* @throws IOException
*/
public void addStudent(String student) throws IOException {
String temp[] = student.split(",");
if (temp.length != 4) {
System.out.println("\u8f93\u5165\u683c\u5f0f\u4e0d\u5408\u6cd5\uff01");
return;
}
Student stu = new Student();
stu.setName(temp[0]);
try {
stu.setChinese(Double.parseDouble(temp[1]));
stu.setMath(Double.parseDouble(temp[2]));
stu.setEnglish(Double.parseDouble(temp[3]));
} catch (Exception e) {
System.out.println("\u8f93\u5165\u6570\u5b57\u4e0d\u5408\u6cd5\uff01");
stu = null;
}
if (stu != null) {
this.saveStudent(stu);
}
}
/**
* \u5220\u9664\u5b66\u751f
*
* @param name
* @throws IOException
* @throws ClassNotFoundException
*/
public void deleteStudent(String name) throws IOException,
ClassNotFoundException {
this.loadStudent();
Student student = null;
for (Student stu : stuList) {
if (stu.getName().equals(name)) {
student = stu;
break;
}
}
if (student == null)
return;
stuList.remove(student);
File f = new File(FILE_PATH);
OutputStream output = new FileOutputStream(f);
MyObjectWriter objOut = new MyObjectWriter(output);
for (Student stu : stuList) {
objOut.writeObject(stu);
}
objOut.close();
output.close();
}
/**
* \u6253\u5370\u6240\u6709\u5b66\u751f\u4fe1\u606f
*/
public void printStudents() {
for (Student st : stuList) {
st.detail();
}
}
/**
* \u52a0\u8f7d\u6240\u6709\u5b66\u751f
*
* @throws IOException
* @throws ClassNotFoundException
*/
public void loadStudent() throws IOException, ClassNotFoundException {
stuList = new ArrayList();
File f = new File(FILE_PATH);
InputStream input = new FileInputStream(f);
ObjectInputStream objInput = new ObjectInputStream(input);
try {
Student obj = (Student) objInput.readObject();
while (obj != null) {
Student student = (Student) obj;
stuList.add(student);
obj = (Student) objInput.readObject();
}
} catch (EOFException e) {
}
objInput.close();
input.close();
}
public static void main(String[] args) throws IOException,
IllegalArgumentException, IllegalAccessException,
ClassNotFoundException {
StuClass stuClass = new StuClass();
while (true) {
System.out.println("-------------\u9009\u62e9\u64cd\u4f5c------------------");
System.out.println("1.\u589e\u52a0 2.\u5220\u9664 3.\u67e5\u770b\u6240\u6709 4.\u9000\u51fa");
Scanner scanner = new Scanner(System.in);
int option = 0;
String sOp = scanner.nextLine();
try {
option = Integer.parseInt(sOp);
} catch (Exception e) {
System.out.println("\u65e0\u6b64\u9009\u9879\uff0c\u91cd\u65b0\u9009\u62e9\uff01");
sOp = scanner.nextLine();
option = Integer.parseInt(sOp);
}
switch (option) {
case 1: {
do {
System.out.println("-------------\u589e\u52a0\u5b66\u751f\u4fe1\u606f---------------");
System.out.println("\u683c\u5f0f:\u59d3\u540d,\u8bed\u6587\u6210\u7ee9,\u6570\u5b66\u6210\u7ee9,\u82f1\u8bed\u6210\u7ee9");
System.out.println("-----------\u8bf7\u8f93\u5165\u6210\u7ee9------------");
String value = scanner.nextLine();
stuClass.addStudent(value.trim());
System.out.println("\u662f\u5426\u7ee7\u7eed\u6dfb\u52a0(y/n)");
String isContinue = scanner.nextLine();
if (!isContinue.equalsIgnoreCase("y"))
break;
} while (true);
}
break;
case 2: {
do {
System.out.println("-------------\u5220\u9664\u5b66\u751f\u4fe1\u606f---------------");
System.out.println("\u8bf7\u8f93\u5165\u5b66\u751f\u59d3\u540d:");
String value = scanner.nextLine();
stuClass.deleteStudent(value.trim());
System.out.println("\u662f\u5426\u7ee7\u7eed\u5220\u9664(y/n)");
String isContinue = scanner.nextLine();
if (!isContinue.equalsIgnoreCase("y"))
break;
} while (true);
}
break;
case 3: {
do {
System.out.println("-------------\u67e5\u8be2\u5b66\u751f\u4fe1\u606f---------------");
stuClass.loadStudent();
stuClass.printStudents();
System.out.println("\u662f\u5426\u7ee7\u7eed(y/n)");
String isContinue = scanner.nextLine();
if (!isContinue.equalsIgnoreCase("y"))
break;
} while (true);
}
break;
case 4:
System.exit(0);
default:
System.out.println("\u65e0\u6b64\u9009\u9879");
break;
}
}
}
}
/**
* \u7528\u6765\u5199\u5165\u5bf9\u8c61\u7684\u7c7b\uff0c\u76f4\u63a5\u7528ObjectOutptStream\uff0c
* \u6bcf\u6b21\u90fd\u4f1a\u5199\u4e00\u4e2a \u6807\u8bc6\u5934\u90e8\uff0c\u5728\u8bfb\u5165\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002
* \u6b64\u7c7b\u6dfb\u52a0\u529f\u80fd\u5224\u65ad\uff0c\u770b\u662f\u5426\u662f\u7b2c\u4e00\u6b21\u5199\u5165\uff0c\u5982\u679c\u662f\u624d\u5199\u6587\u4ef6\u5934\uff0c
* \u5426\u5219\u4e0d\u5199\u3002
*
*/
class MyObjectWriter extends ObjectOutputStream {
public MyObjectWriter(OutputStream out) throws IOException {
super(out);
}
@Override
public void writeStreamHeader() throws IOException {
if (checkFile())
super.writeStreamHeader();
else
super.reset();
}
public boolean checkFile() {
File f = new File("D:\\data.dat");
if (f.length() > 0) {
return false;
}
return true;
}
}
-----------Student.java-----------------
import java.io.Serializable;
class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = -553149347006327008L;
// \u59d3\u540d
private String name;
// \u82f1\u8bed\u6210\u7ee9
private double english;
// \u6570\u5e8f\u6210\u7ee9
private double math;
// \u8bed\u6587\u6210\u7ee9
private double chinese;
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void detail() {
System.out.print("\u59d3\u540d:" + this.name + "\t");
System.out.print("\u8bed\u6587:" + this.chinese + "\t");
System.out.print("\u82f1\u8bed:" + this.english + "\t");
System.out.println("\u6570\u5e8f:" + this.math);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class BitChanger {
public static void main(String[] args) {
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exception e){}
String s="HELLO";
System.out.println(s);
System.out.println(toBinaryString(s));
System.out.println(toHexString(s));
System.out.println(changeBit(toBinaryString(s),12));
System.out.println(toNormalString(changeBit(toBinaryString(s),12)));
System.out.println(toHexString(toNormalString(changeBit(toBinaryString(s),12))));
JFrame f=new JFrame("Bit changer v1.0");
f.setSize(433,230);
f.setDefaultCloseOperation(3);
f.getContentPane().setLayout(null);
JLabel l1=new JLabel("Enter a string:");
final JTextField input=new JTextField(20);
final JLabel binary=new JLabel();
final JLabel hex=new JLabel();
JLabel l2=new JLabel("Enter the Bit number to flip:");
final JTextField flip=new JTextField(5);
JButton change=new JButton("<html>Change<br>Bit");
JButton exit=new JButton("Exit");
l1.setBounds(10,10,100,25);
input.setBounds(110,10,100,25);
binary.setBounds(10,45,400,25);
hex.setBounds(10,80,400,25);
l2.setBounds(10,115,208,25);
flip.setBounds(220,115,40,25);
change.setBounds(270,115,68,45);
exit.setBounds(123,150,60,30);
f.add(l1);
f.add(l2);
f.add(input);
f.add(binary);
f.add(hex);
f.add(flip);
f.add(change);
f.add(exit);
f.setVisible(true);
binary.setBorder(BorderFactory.createLineBorder(Color.blue));
hex.setBorder(BorderFactory.createLineBorder(Color.red));
input.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent e) {
binary.setText(toBinaryString(input.getText()));
hex.setText(toHexString(input.getText()));
}});
change.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try{
int id=Integer.parseInt(flip.getText().trim());
input.setText(toNormalString(changeBit(toBinaryString(input.getText()),id)));
}catch(Exception ex){}
}});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}});
}
private static String toNormalString(String binary){
String[] sp=binary.split(" ");
String v="";
for(int i=0; i<sp.length; i++){
v+=(char)Integer.valueOf(sp[i],2).intValue();
}
return v;
}
private static String changeBit(String s,int bi){
if(s.isEmpty())return s;
int cid=bi/8;
int bid=bi%8;
char ch=s.charAt(cid*9+bid-1);
ch=ch=='0'?'1':'0';
s=s.substring(0,cid*9+bid-1)+ch+s.substring(cid*9+bid);
return s;
}
private static String toBinaryString(String s) {
if(s.isEmpty())return s;
String v="";
for(int i=0; i<s.length(); i++){
String bs=Integer.toBinaryString(s.charAt(i));
while(bs.length()<8)
bs='0'+bs;
v+=bs+" ";
}
return v;
}
private static String toHexString(String s){
if(s.isEmpty())return s;
String v="";
for(int i=0; i<s.length(); i++){
String bs=Integer.toHexString(s.charAt(i));
v+=" "+bs.toUpperCase()+" ";
}
return v;
}
}
绛旓細C.鏈鍚庢槸jdk鐗堟湰鐨勯棶棰锛屼綔涓轰竴闂ㄤ紒涓氱骇鐨勭紪绋嬭瑷锛java涓鐩村湪涓嶆柇鐨鍗囩骇锛岀浉搴旂殑jdk涔熶竴鐩村湪鏇存柊锛屽鏋滀笅杞戒簡涓嶉傚悎鏈湴鍏朵粬杞欢鎴栧疄闄呴渶姹傜殑jdk,閭d箞寰堝彲鑳戒細閫犳垚杞欢涓嶅吋瀹圭殑闂锛屼篃浼氬湪绋嬪簭涓瓨鍦╞ug鐨勯殣鎮o紝鎵浠ュ湪閫夋嫨jdk鐗堟湰鏃朵竴瀹氳鑰冭檻瀹為檯寮鍙戠幆澧冿紝鏍规嵁瀹為檯闇姹傛潵閫夋嫨銆2.jdk鐜閰嶇疆鍑洪敊 A....
绛旓細杩欎竴鐐归潪甯搁潪甯哥殑鐗规畩鍜岄噸瑕侊紒Java寮傚父澶勭悊鐨勭洰鐨勬槸鎻愰珮绋嬪簭鐨勫仴澹э紝浣犲彲浠ュ湪catch鍜宖inally浠g爜鍧椾腑缁欑▼搴忎竴涓慨姝f満浼氾紝浣垮緱绋嬪簭涓嶅洜寮傚父鑰岀粓姝㈡垨鑰呮祦绋嬪彂鐢熶互澶栫殑鏀瑰彉銆傚悓鏃讹紝閫氳繃鑾峰彇Java寮傚父淇℃伅锛屼篃涓虹▼搴忕殑寮鍙戠淮鎶ゆ彁渚涗簡鏂逛究锛屼竴鑸氳繃寮傚父淇℃伅灏卞緢蹇氨鑳芥壘鍒板嚭鐜板紓甯鐨勯棶棰锛堜唬鐮侊級鎵鍦ㄣ侸ava寮傚父澶勭悊...
绛旓細LZ浣犲ソ锛佷綘杩欎釜鐨绋嬪簭for寰幆鍚庨潰澶氫釜 鈥滃垎鍙封濓紝鍘绘帀鈥滃垎鍙封濆氨鍙互浜嗭紝鍒氬垰涔熺敤cmd娴嬭瘯浜嗕竴涓嬶紝杩愯缁撴灉涔熷凡缁忚创涓婁簡銆傝阿璋㈤噰绾炽
绛旓細" + name+ ",鎬у埆:" + sex + ",骞撮緞:" + age); } public static void main(String []args){ Student stu1 = new Student("zw", "boy", 21); Student stu2 = new Student("lj", "girl", 20); stu1.setAge(...
绛旓細褰揝UN鍏徃1995骞存寮忎互Java杩欎釜鍚嶅瓧鎺ㄥ嚭鐨勬椂鍊欙紝鍑犱箮鎵鏈夌殑WEB寮鍙戜汉鍛橀兘蹇冪敓鎰熷徆锛氬櫌锛岃繖姝f槸鎴戞兂瑕佺殑锛佷簬鏄疛ava鎴愪簡涓棰楄鐪肩殑鏄庢槦锛屼笐灏忛腑涓涓嬩簡鍙樻垚浜嗙櫧澶╅箙銆俒缂栬緫鏈]Java涓昏鐗规 1銆丣ava璇█鏄畝鍗曠殑銆侸ava璇█鐨勮娉曚笌C璇█鍜孋++璇█寰堟帴杩戯紝浣垮緱澶у鏁绋嬪簭鍛樺緢瀹规槗瀛︿範鍜屼娇鐢↗ava銆傚彟涓鏂归潰锛...
绛旓細杩欎箞闀跨殑浠g爜娈靛お闅炬壘浜嗭紝浣犵湅涓婨clipse鎻愮ず閭d竴琛屾湁闂涓嶅氨鍙互浜嗗悧锛熸垨鑰呮潵涓涓柇鐐规祴璇曠殑
绛旓細绾跨▼鐨勬椿鍔ㄧ敱涓缁勬柟娉曟潵鎺у埗銆 Java璇█鏀寔澶氫釜绾跨▼鐨勫悓鏃舵墽琛岋紝骞舵彁渚涘绾跨▼涔嬮棿鐨勫悓姝ユ満鍒讹紙鍏抽敭瀛椾负synchronized锛夈11銆丣ava璇█鏄姩鎬佺殑銆侸ava璇█鐨勮璁$洰鏍囦箣涓鏄傚簲浜庡姩鎬佸彉鍖栫殑鐜銆Java绋嬪簭闇瑕佺殑绫昏兘鍔ㄦ佸湴琚浇鍏ュ埌杩愯鐜锛屼篃鍙互閫氳繃缃戠粶鏉ヨ浇鍏ユ墍闇瑕佺殑绫汇傝繖涔熸湁鍒╀簬杞欢鐨鍗囩骇銆傚彟澶栵紝Java...
绛旓細绗竴涓猚lass public class NameTokenizer { private String lastname,middlename,firstname;public NameTokenizer(String name){ java.util.StringTokenizer st = new java.util.StringTokenizer(name);firstname = st.nextToken();middlename = st.nextToken();lastname = st.nextToken();} public ...
绛旓細鍚庡崐涓闂浣犳槸璇存兂鍐欎竴涓暟鎹簱杩樻槸杩炴帴鏁版嵁搴撶殑绋嬪簭锛熷鏋滀綘鎯冲啓涓涓暟鎹簱閭f垜鐪熷府涓嶄簡浣狅紝濡傛灉浣犳兂鍐欎竴涓簲鐢ㄥ埌鏁版嵁搴撶殑绋嬪簭鎴戝彲浠ュ憡璇変綘锛屽幓瀛﹀jdbc,odbc锛屽仛缃戠珯鐨勮瘽鍘诲JNDI鍜孞STL锛岃繖浜涙妧鏈兘鏄繛鎺ユ暟鎹簱鐢ㄧ殑銆傝繖涓嶆槸涓鍙ヤ袱鍙ュ彲浠ヨ鐨勬竻鐨 鍥炵瓟3锛氳蒋浠跺啓濂戒簡瑕佺湅鏈夋病鏈夊埗浣滄垚瀹夎绋嬪簭...
绛旓細鍩烘湰涓婄湅杩囨潵锛屽ぇ鑷寸殑鎰忔濇槸椤甸潰涓婄敤涓涓猯istvew鍘讳繚瀛樼浉鍏崇殑鎵嬫満绯荤粺淇℃伅锛岀劧鍚庡綋鐢ㄦ埛鐐瑰嚮鏌愪釜item鏃讹紝椤甸潰涓婂憟鐜拌item鐨勪俊鎭紝鐒跺悗鏈変簺鍦版柟鎴戝姞涓嬫敞閲 public class System extends Activity implements OnItemClickListener{ private static final String TAG = "System";ListView itemlist = null;List<...