-
05-13-2014, 07:00 AM #11Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
Mã nguồn PHP:[color=#000000]
[/color][color=#007700][[/color][color=#0000bb]spoiler[/color][color=#007700]=[/color][color=#0000bb]spoiler[/color][color=#007700]][/[/color][color=#0000bb]spoiler[/color][color=#007700]][/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]datainputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]dataoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; class [/color][color=#0000bb]sinhvien [/color][color=#007700]{private [/color][color=#0000bb]string ten[/color][color=#007700];private [/color][color=#0000bb]int namsinh[/color][color=#007700];private [/color][color=#0000bb]float diemtb[/color][color=#007700];public [/color][color=#0000bb]string getten[/color][color=#007700]() {return [/color][color=#0000bb]ten[/color][color=#007700];}public [/color][color=#0000bb]void setten[/color][color=#007700]([/color][color=#0000bb]string ten[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]ten[/color][color=#007700];}public [/color][color=#0000bb]int getnamsinh[/color][color=#007700]() {return [/color][color=#0000bb]namsinh[/color][color=#007700];}public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]int namsinh[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700];}public [/color][color=#0000bb]float getdiemtb[/color][color=#007700]() {return [/color][color=#0000bb]diemtb[/color][color=#007700];}public [/color][color=#0000bb]void setdiemtb[/color][color=#007700]([/color][color=#0000bb]float diemtb[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]diemtb [/color][color=#007700]= [/color][color=#0000bb]diemtb[/color][color=#007700];} public [/color][color=#0000bb]void ghithongtin[/color][color=#007700]() [/color][color=#0000bb]throws ioexception [/color][color=#007700]{[/color][color=#0000bb]dataoutputstream dataout[/color][color=#007700];try {[/color][color=#0000bb]dataout [/color][color=#007700]= new [/color][color=#0000bb]dataoutputstream[/color][color=#007700](new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\thongtin.dat"[/color][color=#007700]));} catch ([/color][color=#0000bb]exception e[/color][color=#007700]) {[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]e[/color][color=#007700]);return;} try {[/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeint[/color][color=#007700]([/color][color=#0000bb]getnamsinh[/color][color=#007700]());[/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeutf[/color][color=#007700]([/color][color=#0000bb]getten[/color][color=#007700]());[/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]getdiemtb[/color][color=#007700]());} catch([/color][color=#0000bb]exception e[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]e[/color][color=#007700]);} [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }} public class [/color][color=#0000bb]ghifilekieunhiphan [/color][color=#007700]{public static [/color][color=#0000bb]void [/color][color=#007700]try() [/color][color=#0000bb]throws ioexception[/color][color=#007700]{[/color][color=#0000bb]sinhvien sinhvien [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap ten: "[/color][color=#007700]);@[/color][color=#0000bb]suppresswarnings[/color][color=#007700]([/color][color=#dd0000]"resource"[/color][color=#007700])[/color][color=#0000bb]scanner scan [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]);[/color][color=#0000bb]sinhvien[/color][color=#007700].[/color][color=#0000bb]setten[/color][color=#007700]([/color][color=#0000bb]scan[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap nam sinh: "[/color][color=#007700]);[/color][color=#0000bb]sinhvien[/color][color=#007700].[/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]scan[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap diem trung binh"[/color][color=#007700]);[/color][color=#0000bb]sinhvien[/color][color=#007700].[/color][color=#0000bb]setdiemtb[/color][color=#007700]([/color][color=#0000bb]scan[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700]()); [/color][color=#0000bb]sinhvien[/color][color=#007700].[/color][color=#0000bb]ghithongtin[/color][color=#007700]();} public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{[/color][color=#0000bb]datainputstream datain [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700];try {[/color][color=#0000bb]datain [/color][color=#007700]= new [/color][color=#0000bb]datainputstream[/color][color=#007700](new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#dd0000]"e:\\thongtin.dat"[/color][color=#007700]));} catch ([/color][color=#0000bb]exception e[/color][color=#007700]) {[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]e[/color][color=#007700]);try();} try { [/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readint[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nam sinh " [/color][color=#007700]+ [/color][color=#0000bb]i[/color][color=#007700]); [/color][color=#0000bb]string d [/color][color=#007700]= [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readutf[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"ten " [/color][color=#007700]+ [/color][color=#0000bb]d[/color][color=#007700]); [/color][color=#0000bb]float diem [/color][color=#007700]= [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"diem tb " [/color][color=#007700]+ [/color][color=#0000bb]diem[/color][color=#007700]); } catch ([/color][color=#0000bb]ioexception exc[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"read error."[/color][color=#007700]); } [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700]();}}[/color]
-
05-17-2014, 07:00 AM #12Junior Member
- Ngày tham gia
- Jun 2015
- Bài viết
- 1
xem hộ mình bài này với:
mình không làm được cho kiem tra xem file có chưa tạo nếu có rồi thì in luôn. có thể nói cho minh phương pháp được không.
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>bai tap (move your mouse to reveal the content)</noscript>
bai tap (open)
bai tap (close)
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
import java.io.datainputstream;
import java.io.dataoutputstream;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.scanner;
class data2byte{
public static void main(string args[]) throws ioexception {
scanner sc=new scanner(system.in);
system.out.println("nhap thong tin sinh vien: ");
system.out.print("ho ten: ");
string ten=sc.nextline();
system.out.print("nam sinh: ");
int namsinh=sc.nextint();
system.out.print("diem tb: ");
float dtb=sc.nextfloat();
sinhvien a=new sinhvien(ten,namsinh,dtb);
a.write();
a.read();
}
}
class sinhvien{
private string ten;
private int namsinh;
private float dtb;
public sinhvien(string ten,int namsinh,float dtb) throws ioexception{
this.namsinh=namsinh;
this.ten=ten;
this.dtb=dtb;
}
void write() throws ioexception{
dataoutputstream dataout = null;
try{
dataout=new dataoutputstream(new fileoutputstream("e:\\/file/sinhvien.dat"));
}catch(exception e1){
system.out.print("khong tao duoc file: ");
}
try{
dataout.writeutf(ten);
dataout.writeint(namsinh);
dataout.writefloat(dtb);;
}catch(exception e2){
system.out.print("viet file bi loi");
}
dataout.close();
}
void read()throws ioexception{
datainputstream datain=null;
try{
datain=new datainputstream(new fileinputstream("e:\\/file/sinhvien.dat"));
}catch(exception e3){
system.out.print("khong mo dc file");
}
try{
ten=datain.readutf();
system.out.println("ho ten: "+ten);
namsinh=datain.readint();
system.out.println("nam sinh: "+namsinh);
dtb=datain.readfloat();
system.out.println("diem trung binh: "+dtb);
}catch(exception e4){
system.out.print("doc file bi loi");
}
datain.close();
}
}
</blockquote></div>
</div>
</div>
-
06-15-2014, 07:00 AM #13Junior Member
- Ngày tham gia
- Apr 2016
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>myunit28 (move your mouse to reveal the content)</noscript>
myunit28 (open)
myunit28 (close)
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package hoc[/color][color=#007700].[/color][color=#0000bb]java[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]datainputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]file[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]dataoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; public class [/color][color=#0000bb]unit28 [/color][color=#007700]{public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{[/color][color=#0000bb]scanner sc [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]);[/color][color=#0000bb]unit28 u [/color][color=#007700]= new [/color][color=#0000bb]unit28[/color][color=#007700]();[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap duong dan file: "[/color][color=#007700]);[/color][color=#0000bb]string path [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();if ([/color][color=#0000bb]u[/color][color=#007700].[/color][color=#0000bb]checkfile[/color][color=#007700]([/color][color=#0000bb]path[/color][color=#007700]) != [/color][color=#0000bb]true[/color][color=#007700]) {[/color][color=#0000bb]u[/color][color=#007700].[/color][color=#0000bb]outputdata[/color][color=#007700]([/color][color=#0000bb]path[/color][color=#007700]);} else {[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"file da ton tai, noi dung file: "[/color][color=#007700]);[/color][color=#0000bb]u[/color][color=#007700].[/color][color=#0000bb]inputdata[/color][color=#007700]([/color][color=#0000bb]path[/color][color=#007700]);}} public [/color][color=#0000bb]boolean checkfile[/color][color=#007700]([/color][color=#0000bb]string filename[/color][color=#007700]) {[/color][color=#0000bb]file f [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]filename[/color][color=#007700]);if ([/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]exists[/color][color=#007700]() == [/color][color=#0000bb]true[/color][color=#007700])return [/color][color=#0000bb]true[/color][color=#007700];elsereturn [/color][color=#0000bb]false[/color][color=#007700]; } public [/color][color=#0000bb]void inputdata[/color][color=#007700]([/color][color=#0000bb]string file[/color][color=#007700]) {try {[/color][color=#0000bb]datainputstream indata [/color][color=#007700]= new [/color][color=#0000bb]datainputstream[/color][color=#007700](new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700]));[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"noi dung file"[/color][color=#007700]);[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]indata[/color][color=#007700].[/color][color=#0000bb]readutf[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]indata[/color][color=#007700].[/color][color=#0000bb]readint[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]indata[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700]());[/color][color=#0000bb]indata[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700]();} catch ([/color][color=#0000bb]ioexception exc[/color][color=#007700]) {[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"co loi xay ra: " [/color][color=#007700]+ [/color][color=#0000bb]exc[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]());}} public [/color][color=#0000bb]void outputdata[/color][color=#007700]([/color][color=#0000bb]string file[/color][color=#007700]) {try {[/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]);[/color][color=#0000bb]sinhvienunit28 sv [/color][color=#007700]= new [/color][color=#0000bb]sinhvienunit28[/color][color=#007700]();[/color][color=#0000bb]dataoutputstream outdata [/color][color=#007700]= new [/color][color=#0000bb]dataoutputstream[/color][color=#007700](new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]file[/color][color=#007700]));[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhap ten sv: "[/color][color=#007700]);[/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]sethovaten[/color][color=#007700]([/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nam sinh: "[/color][color=#007700]);[/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"diem tb: "[/color][color=#007700]);[/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setdiemtb[/color][color=#007700]([/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700]());[/color][color=#0000bb]outdata[/color][color=#007700].[/color][color=#0000bb]writeutf[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]gethovaten[/color][color=#007700]());[/color][color=#0000bb]outdata[/color][color=#007700].[/color][color=#0000bb]writeint[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getnamsinh[/color][color=#007700]());[/color][color=#0000bb]outdata[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getdiemtb[/color][color=#007700]());[/color][color=#0000bb]outdata[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } catch ([/color][color=#0000bb]ioexception exc[/color][color=#007700]) {[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"co loi xay ra: " [/color][color=#007700]+ [/color][color=#0000bb]exc[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]());}}} class [/color][color=#0000bb]sinhvienunit28 [/color][color=#007700]{private [/color][color=#0000bb]string hovaten[/color][color=#007700];private [/color][color=#0000bb]int namsinh[/color][color=#007700];private [/color][color=#0000bb]float diemtb[/color][color=#007700]; public [/color][color=#0000bb]string gethovaten[/color][color=#007700]() {return [/color][color=#0000bb]hovaten[/color][color=#007700];} public [/color][color=#0000bb]void sethovaten[/color][color=#007700]([/color][color=#0000bb]string hovaten[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hovaten [/color][color=#007700]= [/color][color=#0000bb]hovaten[/color][color=#007700];} public [/color][color=#0000bb]int getnamsinh[/color][color=#007700]() {return [/color][color=#0000bb]namsinh[/color][color=#007700];} public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]int namsinh[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700];} public [/color][color=#0000bb]float getdiemtb[/color][color=#007700]() {return [/color][color=#0000bb]diemtb[/color][color=#007700];} public [/color][color=#0000bb]void setdiemtb[/color][color=#007700]([/color][color=#0000bb]float diemtb[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]diemtb [/color][color=#007700]= [/color][color=#0000bb]diemtb[/color][color=#007700];} public [/color][color=#0000bb]sinhvienunit28[/color][color=#007700]() {[/color][color=#ff8000]// todo auto-generated constructor stub[/color][color=#007700]} [/color][color=#0000bb]sinhvienunit28[/color][color=#007700]([/color][color=#0000bb]string hoten[/color][color=#007700], [/color][color=#0000bb]int namsinh[/color][color=#007700], [/color][color=#0000bb]float diemtb[/color][color=#007700]) {[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hovaten [/color][color=#007700]= [/color][color=#0000bb]hoten[/color][color=#007700];[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700];[/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]diemtb [/color][color=#007700]= [/color][color=#0000bb]diemtb[/color][color=#007700];}}[/color]
</div>
</div>
-
07-14-2014, 07:00 AM #14Junior Member
- Ngày tham gia
- Apr 2014
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>nộp bài (move your mouse to reveal the content)</noscript>
nộp bài (open)
nộp bài (close)
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package javaandroidvn[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]datainputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]dataoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]file[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filenotfoundexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; public class [/color][color=#0000bb]javaandroid6 [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]string inputfile [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhập đường dẫn file:"[/color][color=#007700]); [/color][color=#0000bb]inputfile [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#ff8000]// kiem tra xem file da ton tai chua [/color][color=#0000bb]file file [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]inputfile[/color][color=#007700]); [/color][color=#0000bb]sinhvien sv [/color][color=#007700]= new [/color][color=#0000bb]sinhvien[/color][color=#007700](); if ([/color][color=#0000bb]file[/color][color=#007700].[/color][color=#0000bb]exists[/color][color=#007700]() == [/color][color=#0000bb]true[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"file đã tồn tại"[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thông tin file:" [/color][color=#007700]+ [/color][color=#0000bb]inputfile[/color][color=#007700]); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]readandshowfile[/color][color=#007700]([/color][color=#0000bb]inputfile[/color][color=#007700]); } else { [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]writefile[/color][color=#007700]([/color][color=#0000bb]inputfile[/color][color=#007700]); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]readandshowfile[/color][color=#007700]([/color][color=#0000bb]inputfile[/color][color=#007700]); } [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }} class [/color][color=#0000bb]sinhvien [/color][color=#007700]{ private [/color][color=#0000bb]string ten[/color][color=#007700]; private [/color][color=#0000bb]int namsinh[/color][color=#007700]; private [/color][color=#0000bb]float dtb[/color][color=#007700]; public [/color][color=#0000bb]string getten[/color][color=#007700]() { return [/color][color=#0000bb]ten[/color][color=#007700]; } public [/color][color=#0000bb]void setten[/color][color=#007700]([/color][color=#0000bb]string ten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]ten[/color][color=#007700]; } public [/color][color=#0000bb]int getnamsinh[/color][color=#007700]() { return [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]int namsinh[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]float getdtb[/color][color=#007700]() { return [/color][color=#0000bb]dtb[/color][color=#007700]; } public [/color][color=#0000bb]void setdtb[/color][color=#007700]([/color][color=#0000bb]float dtb[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]dtb [/color][color=#007700]= [/color][color=#0000bb]dtb[/color][color=#007700]; } public [/color][color=#0000bb]void writefile[/color][color=#007700]([/color][color=#0000bb]string inputfile[/color][color=#007700]) { [/color][color=#0000bb]dataoutputstream dataout [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; try { [/color][color=#0000bb]dataout [/color][color=#007700]= new [/color][color=#0000bb]dataoutputstream[/color][color=#007700](new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]inputfile[/color][color=#007700])); } catch ([/color][color=#0000bb]filenotfoundexception e[/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"can not file"[/color][color=#007700]); [/color][color=#ff8000]// return; // todo auto-generated catch block // e.printstacktrace(); [/color][color=#007700]} [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); try { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"họ tên:"[/color][color=#007700]); [/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeutf[/color][color=#007700]([/color][color=#0000bb]ten[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"năm sinh:"[/color][color=#007700]); [/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeint[/color][color=#007700]([/color][color=#0000bb]namsinh[/color][color=#007700]); [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"điểm trung bình:"[/color][color=#007700]); [/color][color=#0000bb]dtb [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700](); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]dtb[/color][color=#007700]); } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#ff8000]// todo auto-generated catch block [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error:" [/color][color=#007700]+ [/color][color=#0000bb]e[/color][color=#007700]); [/color][color=#ff8000]// e.printstacktrace(); [/color][color=#007700]} [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } public [/color][color=#0000bb]void readandshowfile[/color][color=#007700]([/color][color=#0000bb]string inputfile[/color][color=#007700]) { [/color][color=#0000bb]datainputstream datain [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; try { [/color][color=#0000bb]datain [/color][color=#007700]= new [/color][color=#0000bb]datainputstream[/color][color=#007700](new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]inputfile[/color][color=#007700])); } catch ([/color][color=#0000bb]filenotfoundexception e[/color][color=#007700]) { [/color][color=#ff8000]// todo auto-generated catch block [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"không thể mở file"[/color][color=#007700]); [/color][color=#ff8000]// e.printstacktrace(); [/color][color=#007700]} try { [/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readutf[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"họ tên:" [/color][color=#007700]+ [/color][color=#0000bb]ten[/color][color=#007700]); [/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readint[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"năm sinh:" [/color][color=#007700]+ [/color][color=#0000bb]namsinh[/color][color=#007700]); [/color][color=#0000bb]dtb [/color][color=#007700]= [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"điểm trung bình:" [/color][color=#007700]+ [/color][color=#0000bb]dtb[/color][color=#007700]); } catch ([/color][color=#0000bb]ioexception e[/color][color=#007700]) { [/color][color=#ff8000]// todo auto-generated catch block [/color][color=#0000bb]e[/color][color=#007700].[/color][color=#0000bb]printstacktrace[/color][color=#007700](); } }}[/color]
</div>
</div>
-
07-16-2014, 07:00 AM #15Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
Mã nguồn PHP:[color=#000000]
package streamdata[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]datainputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]dataoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filenotfoundexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];class [/color][color=#0000bb]hello[/color][color=#007700]{[/color][color=#0000bb]string name[/color][color=#007700];[/color][color=#0000bb]long birth[/color][color=#007700];[/color][color=#0000bb]double mark[/color][color=#007700]; }public class [/color][color=#0000bb]hocsinh [/color][color=#007700]{static [/color][color=#0000bb]scanner inp[/color][color=#007700];public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception[/color][color=#007700]{[/color][color=#0000bb]hello a[/color][color=#007700]=new [/color][color=#0000bb]hello[/color][color=#007700]();[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]name[/color][color=#007700]=[/color][color=#dd0000]"truong quang ngu"[/color][color=#007700];[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]birth[/color][color=#007700]=[/color][color=#0000bb]1995[/color][color=#007700];[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]mark[/color][color=#007700]=[/color][color=#0000bb]9.05[/color][color=#007700];[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]name[/color][color=#007700]+[/color][color=#dd0000]"
"[/color][color=#007700]+[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]birth[/color][color=#007700]+[/color][color=#dd0000]"
"[/color][color=#007700]+[/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]mark[/color][color=#007700]);[/color][color=#0000bb]dataoutputstream dataout[/color][color=#007700];[/color][color=#0000bb]datainputstream datain[/color][color=#007700];[/color][color=#ff8000]//hoi dia chi luu file[/color][color=#0000bb]string add[/color][color=#007700];[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"let's input address,please..."[/color][color=#007700]);[/color][color=#0000bb]inp[/color][color=#007700]=new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]);[/color][color=#0000bb]add[/color][color=#007700]=[/color][color=#0000bb]inp[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); try{ [/color][color=#ff8000]//neu file da ton tai thi cho in ra. [/color][color=#0000bb]datain [/color][color=#007700]=new [/color][color=#0000bb]datainputstream[/color][color=#007700](new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]add[/color][color=#007700]));try{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readutf[/color][color=#007700]());}catch([/color][color=#0000bb]exception e[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"no utf"[/color][color=#007700]);}try{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readlong[/color][color=#007700]());}catch([/color][color=#0000bb]exception e[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"no long"[/color][color=#007700]);}try{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readdouble[/color][color=#007700]());}catch([/color][color=#0000bb]exception e[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"no double"[/color][color=#007700]);} }catch([/color][color=#0000bb]filenotfoundexception e[/color][color=#007700]){[/color][color=#ff8000]//luc nay file chua ton tai, ta tao file moi va nhap du lieu cho no.[/color][color=#007700]try{[/color][color=#0000bb]dataout[/color][color=#007700]=new [/color][color=#0000bb]dataoutputstream[/color][color=#007700](new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]add[/color][color=#007700]));}catch([/color][color=#0000bb]exception e1[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error 1"[/color][color=#007700]);return;} [/color][color=#ff8000]//input data[/color][color=#007700]try{[/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeutf[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]name[/color][color=#007700]);[/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writelong[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]birth[/color][color=#007700]);[/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writedouble[/color][color=#007700]([/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]mark[/color][color=#007700]);}catch([/color][color=#0000bb]exception e2[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error 2"[/color][color=#007700]);} [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"end 1"[/color][color=#007700]);[/color][color=#ff8000]//print[/color][color=#007700]try{[/color][color=#0000bb]datain [/color][color=#007700]=new [/color][color=#0000bb]datainputstream[/color][color=#007700](new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]add[/color][color=#007700]));}catch([/color][color=#0000bb]exception e3[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error 3"[/color][color=#007700]);return;} try{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readutf[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readlong[/color][color=#007700]());[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readdouble[/color][color=#007700]());}catch([/color][color=#0000bb]exception e4[/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error 4"[/color][color=#007700]);}[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"end 2"[/color][color=#007700]);}}}[/color]
-
08-06-2014, 07:00 AM #16Junior Member
- Ngày tham gia
- Jul 2015
- Bài viết
- 12
ai giải thích giúp mình tại sao các lệnh write và read phải theo thứ tự thì mới chạy nếu không sẽ ra kết quả sai?
ví dụ: dout.writeutf(a);
dout.writefloat(b);
khi xuất:
din.readutf();
din.readfloat();
(đổi readfloat lên trước thì nó đọc sai).
-
08-18-2014, 07:00 AM #17Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
ai giải thích giúp mình tại sao các lệnh write và read phải theo thứ tự thì mới chạy nếu không sẽ ra kết quả sai?
ví dụ: dout.writeutf(a);
dout.writefloat(b);
khi xuất:
din.readutf();
din.readfloat();
(đổi readfloat lên trước thì nó đọc sai).
| dưỡng da
-
09-13-2014, 07:00 AM #18Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
Gửi bởi thanhpl
-
09-16-2014, 07:00 AM #19Junior Member
- Ngày tham gia
- Feb 2014
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>btvn bai 28 (move your mouse to reveal the content)</noscript>
btvn bai 28 (open)
btvn bai 28 (close)
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package baitapvenha[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]datainputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]dataoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]file[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileinputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]filenotfoundexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]fileoutputstream[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]io[/color][color=#007700].[/color][color=#0000bb]ioexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];[/color][color=#ff8000]/*** bài tập về nhà: tạo đối 1 tượng sinhvien gồm các thuộc tính: tên, năm sinh,* điểm trung bình. ghi các thuộc tính này vào file theo kiểu nhị phân. đọc file* in thông tin ra ngoài. nếu chạy lại chương trình mà file đã tồn tại thì không* tạo đối tượng mới nữa mà in luôn ra thông tin đối tượng đó được đọc từ file!*/[/color][color=#007700]public class [/color][color=#0000bb]bai28 [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]bai28 test [/color][color=#007700]= new [/color][color=#0000bb]bai28[/color][color=#007700](); [/color][color=#0000bb]sinhvien28 sv [/color][color=#007700]= new [/color][color=#0000bb]sinhvien28[/color][color=#007700](); [/color][color=#0000bb]string partfile [/color][color=#007700]= [/color][color=#dd0000]"e:\\thongtinsv.txt"[/color][color=#007700]; [/color][color=#0000bb]test[/color][color=#007700].[/color][color=#0000bb]writefile[/color][color=#007700]([/color][color=#0000bb]partfile[/color][color=#007700], [/color][color=#0000bb]sv[/color][color=#007700]); [/color][color=#ff8000]/*system.out.println("
thong tin sinh vien duoc doc tu file: "); test.readfile(partfile, sv); */ [/color][color=#007700]} public [/color][color=#0000bb]void writefile[/color][color=#007700]([/color][color=#0000bb]string partfile[/color][color=#007700], [/color][color=#0000bb]sinhvien28 sv[/color][color=#007700]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]file f [/color][color=#007700]= new [/color][color=#0000bb]file[/color][color=#007700]([/color][color=#0000bb]partfile[/color][color=#007700]); [/color][color=#0000bb]dataoutputstream dataout [/color][color=#007700]= [/color][color=#0000bb]null[/color][color=#007700]; if (![/color][color=#0000bb]f[/color][color=#007700].[/color][color=#0000bb]exists[/color][color=#007700]()) { try { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nhap thong tin sinh vien ghi vao file: "[/color][color=#007700]); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]nhapsv[/color][color=#007700](); [/color][color=#0000bb]dataout [/color][color=#007700]= new [/color][color=#0000bb]dataoutputstream[/color][color=#007700](new [/color][color=#0000bb]fileoutputstream[/color][color=#007700]([/color][color=#0000bb]partfile[/color][color=#007700])); } catch ([/color][color=#0000bb]filenotfoundexception e[/color][color=#007700]) { [/color][color=#ff8000]// todo auto-generated catch block [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"error: " [/color][color=#007700]+ [/color][color=#0000bb]e[/color][color=#007700]); } [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeutf[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getten[/color][color=#007700]()); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writeint[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getnamsinh[/color][color=#007700]()); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getdtoan[/color][color=#007700]()); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getdly[/color][color=#007700]()); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getdhoa[/color][color=#007700]()); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]writefloat[/color][color=#007700]([/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getdiemtb[/color][color=#007700]()); [/color][color=#0000bb]dataout[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); } [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"
thong tin sinh vien duoc doc tu file: "[/color][color=#007700]); [/color][color=#0000bb]readfile[/color][color=#007700]([/color][color=#0000bb]partfile[/color][color=#007700], [/color][color=#0000bb]sv[/color][color=#007700]); } public [/color][color=#0000bb]void readfile[/color][color=#007700]([/color][color=#0000bb]string partfile[/color][color=#007700], [/color][color=#0000bb]sinhvien28 sv[/color][color=#007700]) [/color][color=#0000bb]throws ioexception [/color][color=#007700]{ [/color][color=#0000bb]datainputstream datain [/color][color=#007700]= new [/color][color=#0000bb]datainputstream[/color][color=#007700](new [/color][color=#0000bb]fileinputstream[/color][color=#007700]([/color][color=#0000bb]partfile[/color][color=#007700])); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setten[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readutf[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"ten sinh vien: " [/color][color=#007700]+ [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getten[/color][color=#007700]()); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readint[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"nam sinh: " [/color][color=#007700]+ [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getnamsinh[/color][color=#007700]()); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setdtoan[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700]()); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setdly[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700]()); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setdhoa[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700]()); [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]setdiemtb[/color][color=#007700]([/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]readfloat[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"diem trung binh: " [/color][color=#007700]+ [/color][color=#0000bb]sv[/color][color=#007700].[/color][color=#0000bb]getdiemtb[/color][color=#007700]()); [/color][color=#0000bb]datain[/color][color=#007700].[/color][color=#0000bb]close[/color][color=#007700](); }} class [/color][color=#0000bb]sinhvien28 [/color][color=#007700]{ private [/color][color=#0000bb]string ten[/color][color=#007700]; private [/color][color=#0000bb]int namsinh[/color][color=#007700]; private [/color][color=#0000bb]float dtoan[/color][color=#007700], [/color][color=#0000bb]dly[/color][color=#007700], [/color][color=#0000bb]dhoa[/color][color=#007700]; private [/color][color=#0000bb]float diemtb[/color][color=#007700]; public [/color][color=#0000bb]void nhapsv[/color][color=#007700]() { [/color][color=#0000bb]scanner sc [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"ten : "[/color][color=#007700]); [/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nam sinh: "[/color][color=#007700]); [/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"toan : "[/color][color=#007700]); [/color][color=#0000bb]dtoan [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"ly : "[/color][color=#007700]); [/color][color=#0000bb]dly [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"hoa : "[/color][color=#007700]); [/color][color=#0000bb]dhoa [/color][color=#007700]= [/color][color=#0000bb]sc[/color][color=#007700].[/color][color=#0000bb]nextfloat[/color][color=#007700](); } public [/color][color=#0000bb]string getten[/color][color=#007700]() { return [/color][color=#0000bb]ten[/color][color=#007700]; } public [/color][color=#0000bb]int getnamsinh[/color][color=#007700]() { return [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]float getdiemtb[/color][color=#007700]() { return [/color][color=#0000bb]diemtb [/color][color=#007700]= ([/color][color=#0000bb]getdtoan[/color][color=#007700]()+[/color][color=#0000bb]getdly[/color][color=#007700]()+[/color][color=#0000bb]getdhoa[/color][color=#007700]()) / [/color][color=#0000bb]3[/color][color=#007700]; } public [/color][color=#0000bb]void setten[/color][color=#007700]([/color][color=#0000bb]string ten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]ten [/color][color=#007700]= [/color][color=#0000bb]ten[/color][color=#007700]; } public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]int namsinh[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]void setdiemtb[/color][color=#007700]([/color][color=#0000bb]float diemtb[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]diemtb [/color][color=#007700]= [/color][color=#0000bb]diemtb[/color][color=#007700]; } public [/color][color=#0000bb]float getdtoan[/color][color=#007700]() { return [/color][color=#0000bb]dtoan[/color][color=#007700]; } public [/color][color=#0000bb]float getdly[/color][color=#007700]() { return [/color][color=#0000bb]dly[/color][color=#007700]; } public [/color][color=#0000bb]float getdhoa[/color][color=#007700]() { return [/color][color=#0000bb]dhoa[/color][color=#007700]; } public [/color][color=#0000bb]void setdtoan[/color][color=#007700]([/color][color=#0000bb]float dtoan[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]dtoan [/color][color=#007700]= [/color][color=#0000bb]dtoan[/color][color=#007700]; } public [/color][color=#0000bb]void setdly[/color][color=#007700]([/color][color=#0000bb]float dly[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]dly [/color][color=#007700]= [/color][color=#0000bb]dly[/color][color=#007700]; } public [/color][color=#0000bb]void setdhoa[/color][color=#007700]([/color][color=#0000bb]float dhoa[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]dhoa [/color][color=#007700]= [/color][color=#0000bb]dhoa[/color][color=#007700]; } }[/color]
</div>
</div>
-
10-04-2014, 07:00 AM #20Junior Member
- Ngày tham gia
- Mar 2014
- Bài viết
- 0
public class bangchinh {
/**
* @param args
*/
public static void main(string[] args) {
// todo auto-generated method stub
sinhvien s=new sinhvien();
s.sethoten("le nguyen hoan vu");
s.setnamsinh("20/12/1992");
s.setdiemtb(9.8);
dataoutputstream dataout;
datainputstream datain;
file filename=new file("e:\\hoanvu.dat");
try {
if (!filename.exists())
{
dataout=new dataoutputstream(new fileoutputstream(filename));
system.out.println("ghi thông tin ra file");
system.out.println("ghi tên sinh viên");
dataout.writeutf(s.gethoten());
system.out.println("ghi ngày tháng năm sinh");
dataout.writeutf(s.getnamsinh());
system.out.println("ghi điểm");
dataout.writedouble(s.getdiemtb());
dataout.close();
}
else
{
datain=new datainputstream(new fileinputstream(filename));
string s1=datain.readutf();
system.out.println("họ tên sinh viên:"+s1);
string s2=datain.readutf();
system.out.println("ngay thang nam sinh:"+s2);
double s3=datain.readdouble();
system.out.println("điểm sinh viên:"+s3);
datain.close();
}
} catch (ioexception e) {
system.out.println("không tạo được file");
return;
}
}}
Căn hộ cao cấp dự án Phú Đông Smart City đầu tư bởi Phú Đông Group tầm nhìn tuyệt mỹ trải rộng view xu hướng đẳng cấp. bán căn hộ Phú Đông Smart City tầm nhìn tuyệt mỹ giá hợp lý địa thế vượt trội....
Căn hộ chung cư Phú Đông Smart City khu hiện đại