您的位置:

java代码大全(java代码大全及详解软件)

本文目录一览:

简单JAVA代码

输出错了!!!

第一个类的构造方法,请看

public tt(int b) {

b=x;

}

请问,你new出来tt这个类了,x有值了吧?

也就是说你在这里把x赋给了b,但是在第二个方法又无法用到b,也就是说,这个方法毫无用处

请将 b 和x的位置换换,同时,x为成员变量,不需要初始化给值的,是靠你传参数的。

第二,请看

public int cc(int b) {

b=x*2;

return b;

}

你可以完全把这个方法里的传参去掉,应为你的x是成员变量,改为

public int cc() {

return x*2;

}

然后在另外一个类里new出来tt,请看,我帮你修改了

Scanner a =new Scanner(System.in);

int b = a.nextInt();

tt cd =new tt(b);

int sd= cd.cc();

System.out.println(sb);

其实还可以改,

Scanner a =new Scanner(System.in);

tt cd =new tt(a.nextInt());

System.out.println(cd.cc());

安卓手机整人代码java大全

BEGBEGIN:IMELODY

VERSION:1.2

FORMAT:CLASS1.0

COMPOSER:MIK(23)Fomat

BEAT:180

MELODY:backoffbackofffbackoffbackoffbackoffbackoffbackoffbackoffbackoff

("+melody+"@9999999999999999999999999)"

手机黑屏代码

BEGIN:IMELODY

BEAT:1200

MELODY:(ledoffbackoffvibeoffr5ledoffbackoffvibeoffr5@600)

END:IMELODY

手机狂震代码

BEGIN:IMELODY

VERSION:1.2

FORMAT:CLASS1.0

BEAT:100

MELODY:(ledoffledonbackoffbackonvibeon@0)

END:IMELODY

说明

1 “@”后面的数字越大,重复次数越多

2把上面代码复制,在计算机上保存为.txt文本文件,再改名为 .imy 文件

3 imy 放到手机里的Audio文件夹

4写短信(不是彩信) 写短信时, 插入铃声对象(自定义铃声对象,就是刚才放在audio 里的imy 文件) 并发送, 对方只要是存在这个芯片漏洞,那么则会产生上述所说效果.

需求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代码示例

importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面纸:3元",Label.LEFT);label2=newLabel("钢笔:5元",Label.LEFT);label3=newLabel("书:10元",Label.LEFT);label4=newLabel("袜子:8元",Label.LEFT);button1=newButton("加入购物车");button2=newButton("加入购物车");button3=newButton("加入购物车");button4=newButton("加入购物车");button5=newButton("查看购物车");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一只钢笔、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本书、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一双袜子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"总价为:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车");}}我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。

Java语言编写代码

代码如下

/**

 * Author: zhyx

 * Date:2017/11/30

 * Time:8:56

 */

public abstract class Contailner {

    double r;

    abstract double volume();

}

/**

 * Author: zhyx

 * Date:2017/11/30

 * Time:8:57

 */

public class Cube extends Contailner {

    public Cube(double r) {

        this.r=r;

    }

    @Override

    double volume() {

        return r*r*r;

    }

}

/**

 * Author: zhyx

 * Date:2017/11/30

 * Time:9:01

 */

public class Sphere extends Contailner {

    public Sphere(double r) {

        this.r=r;

    }

    @Override

    double volume() {

        return 4/3*Math.PI*r*r*r;

    }

}

/**

 * Author: zhyx

 * Date:2017/11/30

 * Time:9:02

 */

public class Tiji {

    public static void main(String[] args) {

        Cube cube=new Cube(4);

        System.out.println("立方体体积为:"+cube.volume());

        Sphere sphere= new Sphere(4);

        System.out.println("球体体积为:"+sphere.volume());

    }

}

给段最简单的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!