Java必备的代码 java语言必背代码大全

java经常要敲的代码

回二楼

公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出武强免费做网站回馈大家。

如果是WEB开发

public static void main(String[] args)

这个几乎看不到,除了部分特殊功能需要调试

至于什么最常见,还真没了解过

几个数据类型是必须的

if(xx.equals(yy)){}

xx==yy

try catch

这个东东永远离不开

给段最简单的java代码 让我新手看一下

最简单的java代码肯定就是这个了,如下:

public class MyFirstApp

{

public static void main(String[] args)

{

System.out.print("Hello world");

}

}

“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!

需求50句以上的JAVA代码,并带注解

Java文件操作大全

1.创建文件夹

//import java.io.*;

File myFolderPath = new File(%%1);

try {

if (!myFolderPath.exists()) {

myFolderPath.mkdir();

}

}

catch (Exception e) {

System.out.println("新建目录操作出错");

e.printStackTrace();

}

2.创建文件

//import java.io.*;

File myFilePath = new File(%%1);

try {

if (!myFilePath.exists()) {

myFilePath.createNewFile();

}

FileWriter resultFile = new FileWriter(myFilePath);

PrintWriter myFile = new PrintWriter(resultFile);

myFile.println(%%2);

resultFile.close();

}

catch (Exception e) {

System.out.println("新建文件操作出错");

e.printStackTrace();

}

3.删除文件

//import java.io.*;

File myDelFile = new File(%%1);

try {

myDelFile.delete();

}

catch (Exception e) {

System.out.println("删除文件操作出错");

e.printStackTrace();

}

4.删除文件夹

//import java.io.*;

File delFolderPath = new File(%%1);

try {

delFolderPath.delete(); //删除空文件夹

}

catch (Exception e) {

System.out.println("删除文件夹操作出错");

e.printStackTrace();

}

5.删除一个文件下夹所有的文件夹

//import java.io.*;

File delfile=new File(%%1);

File[] files=delfile.listFiles();

for(int i=0;ifiles.length;i++){

if(files[i].isDirectory()){

files[i].delete();

}

}

6.清空文件夹

//import java.io.*;

File delfilefolder=new File(%%1);

try {

if (!delfilefolder.exists()) {

delfilefolder.delete();

}

delfilefolder.mkdir();

}

catch (Exception e) {

System.out.println("清空目录操作出错");

e.printStackTrace();

}

7.读取文件

//import java.io.*;

// 逐行读取数据

FileReader fr = new FileReader(%%1);

BufferedReader br = new BufferedReader(fr);

String %%2 = br.readLine();

while (%%2 != null) {

%%3

%%2 = br.readLine();

}

br.close();

fr.close();

8.写入文件

//import java.io.*;

// 将数据写入文件

try {

FileWriter fw = new FileWriter(%%1);

fw.write(%%2);

fw.flush();

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

9.写入随机文件

//import java.io.*;

try {

RandomAcessFile logFile=new RandomAcessFile(%%1,"rw");

long lg=logFile.length();

logFile.seek(%%2);

logFile.writeByte(%%3);

}catch(IOException ioe){

System.out.println("无法写入文件:"+ioe.getMessage());

}

10.读取文件属性

//import java.io.*;

// 文件属性的取得

File af = new File(%%1);

if (af.exists()) {

System.out.println(f.getName() + "的属性如下: 文件长度为:" + f.length());

System.out.println(f.isFile() ? "是文件" : "不是文件");

System.out.println(f.isDirectory() ? "是目录" : "不是目录");

System.out.println(f.canRead() ? "可读取" : "不");

System.out.println(f.canWrite() ? "是隐藏文件" : "");

System.out.println("文件夹的最后修改日期为:" + new Date(f.lastModified()));

} else {

System.out.println(f.getName() + "的属性如下:");

System.out.println(f.isFile() ? "是文件" : "不是文件");

System.out.println(f.isDirectory() ? "是目录" : "不是目录");

System.out.println(f.canRead() ? "可读取" : "不");

System.out.println(f.canWrite() ? "是隐藏文件" : "");

System.out.println("文件的最后修改日期为:" + new Date(f.lastModified()));

}

if(f.canRead()){

%%2

}

if(f.canWrite()){

%%3

}

11.写入属性

//import java.io.*;

File filereadonly=new File(%%1);

try {

boolean b=filereadonly.setReadOnly();

}

catch (Exception e) {

System.out.println("拒绝写访问:"+e.printStackTrace());

}

12.枚举一个文件夹中的所有文件

//import java.io.*;

//import java.util.*;

LinkedListString folderList = new LinkedListString();

folderList.add(%%1);

while (folderList.size() 0) {

File file = new File(folderList.peek());

folderList.removeLast();

File[] files = file.listFiles();

ArrayListFile fileList = new ArrayListFile();

for (int i = 0; i files.length; i++) {

if (files[i].isDirectory()) {

folderList.add(files[i].getPath());

} else {

fileList.add(files[i]);

}

}

for (File f : fileList) {

%%2=f.getAbsoluteFile();

%%3

}

}

13.复制文件夹

//import java.io.*;

//import java.util.*;

LinkedListString folderList = new LinkedListString();

folderList.add(%%1);

LinkedListString folderList2 = new LinkedListString();

folderList2.add(%%2+ %%1.substring(%%1.lastIndexOf("\\")));

while (folderList.size() 0) {

(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹

File folders = new File(folderList.peek());

String[] file = folders.list();

File temp = null;

try {

for (int i = 0; i file.length; i++) {

if (folderList.peek().endsWith(File.separator)) {

temp = new File(folderList.peek() + File.separator

+ file[i]);

} else {

temp = new File(folderList.peek() + File.separator

+ file[i]);

}

if (temp.isFile()) {

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(

folderList2.peek() + File.separator

+ (temp.getName()).toString());

byte[] b = new byte[5120];

int len;

while ((len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

}

if (temp.isDirectory()) {// 如果是子文件夹

for (File f : temp.listFiles()) {

if (f.isDirectory()) {

folderList.add(f.getPath());

folderList2.add(folderList2.peek()

+ File.separator + f.getName());

}

}

}

}

} catch (Exception e) {

//System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

folderList.removeFirst();

folderList2.removeFirst();

}

14.复制一个文件夹下所有的文件夹到另一个文件夹下

//import java.io.*;

//import java.util.*;

File copyfolders=new File(%%1);

File[] copyfoldersList=copyfolders.listFiles();

for(int k=0;kcopyfoldersList.length;k++){

if(copyfoldersList[k].isDirectory()){

ArrayListStringfolderList=new ArrayListString();

folderList.add(copyfoldersList[k].getPath());

ArrayListStringfolderList2=new ArrayListString();

folderList2.add(%%2+"/"+copyfoldersList[k].getName());

for(int j=0;jfolderList.length;j++){

(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹

File folders=new File(folderList.get(j));

String[] file=folders.list();

File temp=null;

try {

for (int i = 0; i file.length; i++) {

if(folderList.get(j).endsWith(File.separator)){

temp=new File(folderList.get(j)+"/"+file[i]);

}

else{

temp=new File(folderList.get(j)+"/"+File.separator+file[i]);

}

FileInputStream input = new FileInputStream(temp);

if(temp.isFile()){

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" +

(temp.getName()).toString());

byte[] b = new byte[5120];

int len;

while ( (len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

}

if(temp.isDirectory()){//如果是子文件夹

folderList.add(folderList.get(j)+"/"+file[i]);

folderList2.add(folderList2.get(j)+"/"+file[i]);

}

}

}

catch (Exception e) {

System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

}

}

}

15.移动文件夹

//import java.io.*;

//import java.util.*;

LinkedListString folderList = new LinkedListString();

folderList.add(%%1);

LinkedListString folderList2 = new LinkedListString();

folderList2.add(%%2 + %%1.substring(%%1.lastIndexOf("\\")));

while (folderList.size() 0) {

(new File(folderList2.peek())).mkdirs(); // 如果文件夹不存在 则建立新文件夹

File folders = new File(folderList.peek());

String[] file = folders.list();

File temp = null;

try {

for (int i = 0; i file.length; i++) {

if (folderList.peek().endsWith(File.separator)) {

temp = new File(folderList.peek() + File.separator

+ file[i]);

} else {

temp = new File(folderList.peek() + File.separator

+ file[i]);

}

if (temp.isFile()) {

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(

folderList2.peek() + File.separator

+ (temp.getName()).toString());

byte[] b = new byte[5120];

int len;

while ((len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

if (!temp.delete())

System.out.println("删除单个文件操作出错!");

}

if (temp.isDirectory()) {// 如果是子文件夹

for (File f : temp.listFiles()) {

if (f.isDirectory()) {

folderList.add(f.getPath());

folderList2.add(folderList2.peek()

+ File.separator + f.getName());

}

}

}

}

} catch (Exception e) {

// System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

folderList.removeFirst();

folderList2.removeFirst();

}

File f = new File(%%1);

if (!f.delete()) {

for (File file : f.listFiles()) {

if (file.list().length == 0) {

System.out.println(file.getPath());

file.delete();

}

}

}

16.移动一个文件夹下所有的文件夹到另一个目录下

//import java.io.*;

//import java.util.*;

File movefolders=new File(%%1);

File[] movefoldersList=movefolders.listFiles();

for(int k=0;kmovefoldersList.length;k++){

if(movefoldersList[k].isDirectory()){

ArrayListStringfolderList=new ArrayListString();

folderList.add(movefoldersList[k].getPath());

ArrayListStringfolderList2=new ArrayListString();

folderList2.add(%%2+"/"+movefoldersList[k].getName());

for(int j=0;jfolderList.length;j++){

(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹

File folders=new File(folderList.get(j));

String[] file=folders.list();

File temp=null;

try {

for (int i = 0; i file.length; i++) {

if(folderList.get(j).endsWith(File.separator)){

temp=new File(folderList.get(j)+"/"+file[i]);

}

else{

temp=new File(folderList.get(j)+"/"+File.separator+file[i]);

}

FileInputStream input = new FileInputStream(temp);

if(temp.isFile()){

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" +

(temp.getName()).toString());

byte[] b = new byte[5120];

int len;

while ( (len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

temp.delete();

}

if(temp.isDirectory()){//如果是子文件夹

folderList.add(folderList.get(j)+"/"+file[i]);

folderList2.add(folderList2.get(j)+"/"+file[i]);

}

}

}

catch (Exception e) {

System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

}

movefoldersList[k].delete();

}

}

17.以一个文件夹的框架在另一个目录创建文件夹和空文件

//import java.io.*;

//import java.util.*;

boolean b=false;//不创建空文件

ArrayListStringfolderList=new ArrayListString();

folderList.add(%%1);

ArrayListStringfolderList2=new ArrayListString();

folderList2.add(%%2);

for(int j=0;jfolderList.length;j++){

(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹

File folders=new File(folderList.get(j));

String[] file=folders.list();

File temp=null;

try {

for (int i = 0; i file.length; i++) {

if(folderList.get(j).endsWith(File.separator)){

temp=new File(folderList.get(j)+"/"+file[i]);

}

else{

temp=new File(folderList.get(j)+"/"+File.separator+file[i]);

}

FileInputStream input = new FileInputStream(temp);

if(temp.isFile()){

if (b) temp.createNewFile();

}

if(temp.isDirectory()){//如果是子文件夹

folderList.add(folderList.get(j)+"/"+file[i]);

folderList2.add(folderList2.get(j)+"/"+file[i]);

}

}

}

catch (Exception e) {

System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

}

18.复制文件

//import java.io.*;

int bytesum = 0;

int byteread = 0;

File oldfile = new File(%%1);

try {

if (oldfile.exists()) { //文件存在时

FileInputStream inStream = new FileInputStream(oldfile); //读入原文件

FileOutputStream fs = new FileOutputStream(new File(%%2,oldfile.getName()));

byte[] buffer = new byte[5120];

int length;

while ( (byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; //字节数 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

}

}

catch (Exception e) {

System.out.println("复制单个文件操作出错");

e.printStackTrace();

}

19.复制一个文件夹下所有的文件到另一个目录

//import java.io.*;

File copyfiles=new File(%%1);

File[] files=copyfiles.listFiles();

for(int i=0;ifiles.length;i++){

if(!files[i].isDirectory()){

int bytesum = 0;

int byteread = 0;

try {

InputStream inStream = new FileInputStream(files[i]); //读入原文件

FileOutputStream fs = new FileOutputStream(new File(%%2,files[i].getName());

byte[] buffer = new byte[5120];

int length;

while ( (byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; //字节数 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

}

inStream.close();

} catch (Exception e) {

System.out.println("复制单个文件操作出错");

e.printStackTrace();

}

}

}

20.提取扩展名

String %%2=%%1.substring(%%1.lastIndexOf(".")+1);

一个完整的可运行的java程序包括哪些基本原代码

Java程序包括2种

1) Java 应用程序,必须具有一个main方法入口

public class Test{

public static void main(String args[]){

}

}

2) Java 小应用程序

Applet类中的四种基本方法用来控制其运行状态:init()、start()、stop()、destroy() ,至少具有init start方法。。


本文题目:Java必备的代码 java语言必背代码大全
当前地址:http://abwzjs.com/article/ddjshog.html