java你好中國代碼 看懂java代碼

請問用java創(chuàng)建一個TXT文件,文件里寫“你好”,代碼怎么寫

import java.io.File;

創(chuàng)新互聯(lián)為企業(yè)提供:成都品牌網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷策劃、小程序設(shè)計、營銷型網(wǎng)站建設(shè)和網(wǎng)站運(yùn)營托管,一站式網(wǎng)絡(luò)營銷整體服務(wù)。實(shí)現(xiàn)不斷獲取潛在客戶之核心目標(biāo),建立了企業(yè)專屬的“營銷型網(wǎng)站”,就用不著再為了獲取潛在客戶而苦惱,相反,客戶會主動找您,生意就找上門來了!

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Io{

public static void main(String [] s){

File filename = new File("F:\\suncity.txt");

String filein="你好!";

RandomAccessFile mm = null;

try {

mm = new RandomAccessFile(filename,"rw");

mm.writeBytes(filein);

} catch (IOException e1) {

// TODO 自動生成 catch 塊

e1.printStackTrace();

} finally{

if(mm!=null){

try {

mm.close();

} catch (IOException e2) {

// TODO 自動生成 catch 塊

e2.printStackTrace();

}

}

}

}

}

java面試題求代碼,最好有注解。。。

你好,代碼如下。需要修改的話,你可以根據(jù)情況修改:

class Info{ // 定義信息類

private String name = "生產(chǎn)者"; // 定義name屬性

private String content = "壓入子彈" ; // 定義content屬性

private boolean flag = false ; // 設(shè)置標(biāo)志位

public synchronized void set(String name,String content){

if(!flag){

try{

super.wait() ;

}catch(InterruptedException e){

e.printStackTrace() ;

}

}

this.setName(name) ; // 設(shè)置名稱

try{

Thread.sleep(300) ;

}catch(InterruptedException e){

e.printStackTrace() ;

}

this.setContent(content) ; // 設(shè)置內(nèi)容

flag = false ; // 改變標(biāo)志位,表示可以取走

super.notify() ;

}

public synchronized void get(){

if(flag){

try{

super.wait() ;

}catch(InterruptedException e){

e.printStackTrace() ;

}

}

try{

Thread.sleep(300) ;

}catch(InterruptedException e){

e.printStackTrace() ;

}

System.out.println(this.getName() +

" -- " + this.getContent()) ;

flag = true ; // 改變標(biāo)志位,表示可以生產(chǎn)

super.notify() ;

}

public void setName(String name){

this.name = name ;

}

public void setContent(String content){

this.content = content ;

}

public String getName(){

return this.name ;

}

public String getContent(){

return this.content ;

}

};

class Producer implements Runnable{ // 通過Runnable實(shí)現(xiàn)多線程

private Info info = null ; // 保存Info引用

public Producer(Info info){

this.info = info ;

}

public void run(){

boolean flag = false ; // 定義標(biāo)記位

for(int i=0;i12;i++){

if(flag){

this.info.set("生產(chǎn)者","壓入子彈") ; // 設(shè)置名稱

flag = false ;

}else{

this.info.set("消費(fèi)者","射出子彈") ; // 設(shè)置名稱

flag = true ;

}

}

}

};

class Consumer implements Runnable{

private Info info = null ;

public Consumer(Info info){

this.info = info ;

}

public void run(){

for(int i=0;i24;i++){

this.info.get() ;

}

}

};

public class ThreadCaseDemo03{

public static void main(String args[]){

Info info = new Info(); // 實(shí)例化Info對象

Producer pro = new Producer(info) ; // 生產(chǎn)者

Consumer con = new Consumer(info) ; // 消費(fèi)者

new Thread(pro).start() ;

new Thread(con).start() ;

}

};

如何使用java開發(fā)二維碼代碼

1: 使用SwetakeQRCode在Java項(xiàng)目中生成二維碼

下載地址

或著

這個是日本人寫的,生成的是我們常見的方形的二維碼

可以用中文

如:5677777ghjjjjj

2: 使用BarCode4j生成條形碼和二維碼

BarCode4j網(wǎng)址:

barcode4j是使用datamatrix的二維碼生成算法,為支持qr的算法

datamatrix是歐美的標(biāo)準(zhǔn),qr為日本的標(biāo)準(zhǔn),

barcode4j一般生成出來是長方形的

如:88777alec000yan

這個博客這方面說的挺清楚的:

3:zxing

zxing 這個是google的

下載地址

Java代碼:

import java.io.File;

import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.client.j2se.MatrixToImageWriter;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.QRCodeWriter;

public class QRCodeEvents {

public static void main(String []args)throws Exception{

String text = "你好";

int width = 100;

int height = 100;

String format = "png";

Hashtable hints= new Hashtable();

hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);

File outputFile = new File("new.png");

MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);

}

}

4:google chart api就有實(shí)現(xiàn)二維碼的方法

利用這個api,使用google appengine進(jìn)行實(shí)現(xiàn)。

5:JS生成二維碼

使用jQuery-qrcode生成二維碼

先簡單說一下jquery-qrcode,這個開源的三方庫(可以從 獲?。?,

qrcode.js 是實(shí)現(xiàn)二維碼數(shù)據(jù)計算的核心類,

jquery.qrcode.js 是把它用jquery方式封裝起來的,用它來實(shí)現(xiàn)圖形渲染,其實(shí)就是畫圖(支持canvas和table兩種方式)

支持的功能主要有:

Js代碼:

text : "" //設(shè)置二維碼內(nèi)容

Js代碼:

render : "canvas",//設(shè)置渲染方式

width : 256, //設(shè)置寬度

height : 256, //設(shè)置高度

typeNumber : -1, //計算模式

correctLevel : QRErrorCorrectLevel.H,//糾錯等級

background : "#ffffff",//背景顏色

foreground : "#000000" //前景顏色

使用方式非常簡單

Js代碼:

jQuery('#output').qrcode({width:200,height:200,correctLevel:0,text:content});

經(jīng)過簡單實(shí)踐,

使用canvas方式渲染性能還是非常不錯的,但是如果用table方式,性能不太理想,特別是IE9以下的瀏覽器,所以需要自行優(yōu)化一下渲染table的方式,這里就不細(xì)述了。

其實(shí)上面的js有一個小小的缺點(diǎn),就是默認(rèn)不支持中文。

這跟js的機(jī)制有關(guān)系,jquery-qrcode這個庫是采用 charCodeAt() 這個方式進(jìn)行編碼轉(zhuǎn)換的,

而這個方法默認(rèn)會獲取它的 Unicode 編碼,一般的解碼器都是采用UTF-8, ISO-8859-1等方式,

英文是沒有問題,如果是中文,一般情況下Unicode是UTF-16實(shí)現(xiàn),長度2位,而UTF-8編碼是3位,這樣二維碼的編解碼就不匹配了。

解決方式當(dāng)然是,在二維碼編碼前把字符串轉(zhuǎn)換成UTF-8,具體代碼如下:

function utf16to8(str) {

var out, i, len, c;

out = "";

len = str.length;

for(i = 0; i len; i++) {

c = str.charCodeAt(i);

if ((c = 0x0001) (c = 0x007F)) {

out += str.charAt(i);

} else if (c 0x07FF) {

out += String.fromCharCode(0xE0 | ((c 12) 0x0F));

out += String.fromCharCode(0x80 | ((c 6) 0x3F));

out += String.fromCharCode(0x80 | ((c 0) 0x3F));

} else {

out += String.fromCharCode(0xC0 | ((c 6) 0x1F));

out += String.fromCharCode(0x80 | ((c 0) 0x3F));

}

}

return out;

}

java中的類 FileReader讀取字符了,其讀取字符需要指定字符的編碼規(guī)則吧,那么是怎么指定的?

out.write(s.getBytes("ISO-8859-1"));

這條語句是說,將s按照ISO-8859-1編碼的方式進(jìn)行編碼轉(zhuǎn)換,而ISO-8859-1編碼是單字節(jié)拉丁字符編碼(即ASCII編碼),會把雙字節(jié)的中文編碼轉(zhuǎn)換成單字節(jié),丟失了一個字節(jié),一般的轉(zhuǎn)換成了單字節(jié)后,該字節(jié)對應(yīng)的十六進(jìn)制數(shù)是3F,該數(shù)對應(yīng)ASCII編碼的?所以,結(jié)果是一串問號;其實(shí),你在把數(shù)據(jù)寫入到文件,已經(jīng)發(fā)生了丟失了數(shù)據(jù)!所以最后是亂碼??!

現(xiàn)在解釋為什么123正常顯示,字符串在java中是以Unicode編碼方式進(jìn)行編碼的,該編碼方式占2個字節(jié),對于123這3個字符,其實(shí)也占有了6個字節(jié),在進(jìn)行編碼轉(zhuǎn)換的過程中,也丟失了一個字節(jié),丟失的是高字節(jié),保留了低字節(jié),同時Unicode編碼是兼容ASCII碼的,所以可以認(rèn)為沒有發(fā)生字節(jié)丟失,讀取的時候,只要是以兼容ASCII碼的編碼方式讀取,基本上不會出現(xiàn)亂碼!

如有不清楚地方,歡迎繼續(xù)追問??!

求一個簡單的java代碼

public class Message {

public static void main(String[] args){

String name;

int age;

System.out.println("請輸入姓名,回車結(jié)束:"); //提示輸入

Scanner sc = new Scanner(System.in);

name = sc.nextLine(); //為變量賦值

System.out.println("請輸入年齡,回車結(jié)束:");

age = sc.nextInt();

System.out.println("姓名:"+name+"\n年齡:"+age); //打印姓名及年齡

}

}

//不知道這樣行么?

當(dāng)前名稱:java你好中國代碼 看懂java代碼
本文地址:http://muchs.cn/article24/hgscce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)站維護(hù)、小程序開發(fā)網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)