最簡(jiǎn)單的java代碼肯定就是這個(gè)了,如下:
為市中等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及市中網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為做網(wǎng)站、網(wǎng)站建設(shè)、市中網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來(lái)我們的java實(shí)驗(yàn)班試聽(tīng),有免費(fèi)的試聽(tīng)課程幫助學(xué)習(xí)java必備基礎(chǔ)知識(shí),有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評(píng)團(tuán)進(jìn)行專業(yè)測(cè)試,幫助測(cè)評(píng)學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來(lái)報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門(mén)java,更好的學(xué)習(xí)java!
不知道是否理解對(duì)了你的意思,大概寫(xiě)了一下:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
public class FileReadAndWrite {
private static final int DEFAULT_BUFFER_SIZE = 1024;
public static void main(String[] args) {
File file = new File("E:/workspace/FileIOTest/src/a.txt");
String str = file2String(file, "UTF-8");
str = str.replace('d', 'f');
string2File(str,"E:/workspace/FileIOTest/src/b.txt");
System.out.println("處理完畢");
}
/**
* 文本文件轉(zhuǎn)換為指定編碼的字符串
*
* @param file
* 文本文件
* @param encoding
* 編碼類(lèi)型
* @return 轉(zhuǎn)換后的字符串
* @throws IOException
*/
public static String file2String(File file, String encoding) {
InputStreamReader reader = null;
StringWriter writer = new StringWriter();
try {
if (encoding == null || "".equals(encoding.trim())) {
reader = new InputStreamReader(new FileInputStream(file),
encoding);
} else {
reader = new InputStreamReader(new FileInputStream(file));
}
// 將輸入流寫(xiě)入輸出流
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
int n = 0;
while (-1 != (n = reader.read(buffer))) {
writer.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 返回轉(zhuǎn)換結(jié)果
if (writer != null)
return writer.toString();
else
return null;
}
/**
* 將字符串寫(xiě)入指定文件(當(dāng)指定的父路徑中文件夾不存在時(shí),會(huì)最大限度去創(chuàng)建,以保證保存成功!)
*
* @param res
* 原字符串
* @param filePath
* 文件路徑
* @return 成功標(biāo)記
*/
public static boolean string2File(String res, String filePath) {
boolean flag = true;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File distFile = new File(filePath);
if (!distFile.getParentFile().exists())
distFile.getParentFile().mkdirs();
bufferedReader = new BufferedReader(new StringReader(res));
bufferedWriter = new BufferedWriter(new FileWriter(distFile));
char buf[] = new char[1024]; // 字符緩沖區(qū)
int len;
while ((len = bufferedReader.read(buf)) != -1) {
bufferedWriter.write(buf, 0, len);
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
flag = false;
return flag;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return flag;
}
}
冒泡法排序:
public class MaoPao {
public static void main(String[] args) {
int a=24,b=45,c=85,d=23,e=87,f=29,g=74,h=96,i=28,j=14;
int arr[]={a,b,c,d,e,f,g,h,i,j};
for (int m = 0; m arr.length; m++) {
for (int k = 0; k arr.length-1; k++) {
if(arr[k]arr[k+1]){
int temp=0;
temp=arr[k];
arr[k]=arr[k+1];
arr[k+1]=temp;
}
}
}
for (int k = 0; k arr.length; k++) {
System.out.println(arr[k]);
}
}
}
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int a=1;
while(a!=0)
{
Scanner input = new Scanner(System.in);
System.out.println("輸入整數(shù)(輸入0結(jié)束)");
a = input.nextInt();
if(a0a1000)
{
int b = a % 10;
int temp = a / 10;
int c = temp % 10;
int d = temp/10;
int result = b + c + d;
System.out.println("該整數(shù)的各位相加是"+result);
}else
{
System.out.println("該整數(shù)不再0和1000之間");
}
}
}
}