導航:首頁 > 編程語言 > java調整格式

java調整格式

發布時間:2025-02-04 23:03:33

㈠ 請問,將java文件的格式進行轉換,由GBK編碼轉UTF-8

try{
Stringstr=newString("xx".getBytes("GBK"),"UTF-8");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}

你可以試試看,希望能幫到你。

㈡ java怎麼設置編碼格式修改成utf-8

㈢ java 如何設置字體格式

Java Swing中可以給每個控制項設置字體格式和其他屬性的設置,示例如下:x0dx0asubmit= new JButton("登陸");x0dx0asubmit.setFont(new Font("宋體", Font.PLAIN, 16));x0dx0a三個參數分別表示: 字體,樣式(粗體,斜體等),字型大小x0dx0a submit.setForeground(Color.RED);x0dx0a這個表示給組件上的文字設置顏色Color.RED表示紅色x0dx0a當然你也可以自己給RGB的值 比如 submit.setForeground(new Color(215,215,200));

㈣ java怎麼更改file文件格式

/**
* @author weict
*
*/
public class ChangeFileCode {
// 讀取的文件
private String fileIn;

// 讀取時文件用的編碼
private String fileInEn;

// 寫出的文件
private String fileOut;

// 寫出時文件用的編碼
private String fileOutEn;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

public void setFileIn(String fileInPath, String fileInEncoding) {
this.setFileIn(fileInPath);
this.setFileInEn(fileInEncoding);
}

public void setFileOut(String fileOutPath, String fileOutEncoding) {
this.setFileOut(fileOutPath);
this.setFileOutEn(fileOutEncoding);
}

public void start() {
String str = this.read(fileIn,fileInEn);
this.write(fileOut, fileOutEn, str);
}

/**
* 讀文件
*
* @param fileName
* @param encoding
*/
private String read(String fileName, String encoding) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), encoding));

String string = "";
String str = "";
while ((str = in.readLine()) != null) {
string += str + "\n";
}
in.close();
System.out.println(string);
return string;
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}

/**
* 寫文件
*
* @param fileName
* 新的文件名
* @param encoding
* 寫出的文件的編碼方式
* @param str
*/
private void write(String fileName, String encoding, String str) {
try {
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileName), encoding));
out.write(str);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public String getFileIn() {
return fileIn;
}

public void setFileIn(String fileIn) {
this.fileIn = fileIn;
}

public String getFileInEn() {
return fileInEn;
}

public void setFileInEn(String fileInEn) {
this.fileInEn = fileInEn;
}

public String getFileOut() {
return fileOut;
}

public void setFileOut(String fileOut) {
this.fileOut = fileOut;
}

public String getFileOutEn() {
return fileOutEn;
}

public void setFileOutEn(String fileOutEn) {
this.fileOutEn = fileOutEn;
}

}

使用例子如下:

Java代碼
import java.io.File;

/**
* @author weict
*
*/
public class Demo {

/**
* @param args
*/
public static void main(String[] args) {
String path = "文件路徑";
File file = new File(path);
String fileCode = JudgeFileCode.getFileCode(file);
if(fileCode.equals("UTF-8")){
ChangeFileCode changeFileCode = new ChangeFileCode();
changeFileCode.setFileIn(file.getPath(), "UTF-8");//如果文件編碼為ANSI用GBK來讀,如果是UTF-8用UTF-8來讀
changeFileCode.setFileOut(file.getPath(), "GBK");//UTF-8則文件編碼為UTF-8, 如果為GBK,編碼為ANSI
changeFileCode.start();
}

}

}

閱讀全文

與java調整格式相關的資料

熱點內容
程序員素質大全 瀏覽:952
激戰伺服器不見了怎麼辦 瀏覽:982
安卓如何退回初始系統 瀏覽:951
用什麼手錶能代替app 瀏覽:705
女程序員熬夜體檢 瀏覽:715
解壓故事校園戀愛又煩惱 瀏覽:555
冰箱壓縮機放多久能啟動 瀏覽:171
軟體演算法發明專利 瀏覽:987
旁氏演算法 瀏覽:765
程序員那麼可愛電視劇免費觀看極速版 瀏覽:747
程序員那麼可愛陸漓孩子保住了嗎 瀏覽:564
如何獲取伺服器dns秒解 瀏覽:663
如何破解大黃蜂的加密文件 瀏覽:11
新概念英語第三冊pdf 瀏覽:401
分項詳細估演算法步驟 瀏覽:436
ipad桌面文件夾放大 瀏覽:891
我的世界基岩版怎麼進國際伺服器 瀏覽:516
福州醫院有沒有解壓艙 瀏覽:470
帶pwm的51單片機 瀏覽:918
ace程序員指南源碼 瀏覽:416