十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
将 string[] args 改成 String[] args。如果是初学Java,记住,Java 标准使用驼峰命令法,所有类名的首字母都是大写。好好加油,刚开始学大家都会犯这些错误
十余年的鄂州网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整鄂州建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“鄂州网站设计”,“鄂州网站推广”以来,每个客户项目都认真落实执行。
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);
给你一个比较简单的吧。以前写的。
共两个类。还只是完成+、-、×、÷运算而已。
GUI只是用了AWT,很简单,相信一看就能懂了。
Calculator.java
public class Calculator{
private String result = "0";
private int op = 0,add = 1,sub = 2,mul = 3,div = 4;
private double stringToDouble(String x){
double y = Double.parseDouble(x);
return y;
}
private void operate(String x){
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op){
case 0:
result = x;
break;
case 1:
result = String.valueOf(y+x1);
break;
case 2:
result = String.valueOf(y-x1);
break;
case 3:
result = String.valueOf(y*x1);
break;
case 4:
if(x1!=0){
result = String.valueOf(y/x1);
}else{
result = "The divisor can't be zero!";
}
break;
}
}
public String opAdd(String x){
operate(x);
op = add;
return result;
}
public String opSubtract(String x){
operate(x);
op = sub;
return result;
}
public String opMultiply(String x){
operate(x);
op = mul;
return result;
}
public String opDivide(String x){
operate(x);
op = div;
return result;
}
public String opEquals(String x){
operate(x);
op = 0;
return result;
}
public void opClean(){
op = 0;
result = "0";
}
}
CalculatorGUI.java
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
public class CalculatorGUI{
private Frame f;
private Panel p1,p2;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;
private TextField tf;
private String s,op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI(){
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
}
public void launchFrame(){
f.setSize(220,160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2,BorderLayout.SOUTH);
f.setVisible(true);
}
public void setTextFieldText_Temp(){
if (tf.getText().length()15 (tf.getText().indexOf(".")==-1 || !s.equals("."))){
tf.setText(tf.getText()+s);
}else{
tf.setText((tf.getText()+s).substring(0,15));
}
}
public void setTextFieldText(){
if(ifOp){
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
}else{
setTextFieldText_Temp();
}
}
public static void main(String[] args){
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}
class myWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}
class setOperator_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
}else if(op.equals("-")){
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
}else if(op.equals("*")){
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
}else if(op.equals("/")){
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
}else if(op.equals("=")){
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
可以使用递归,我就写一个static方法剩下的语句你应该会写
public static int sum(int num)
{int k=0;
if(num=10)k=6;
else if(num10num=20)k=2;
else if(num20num40)k=1;
if(num=40)return 100;
else if(num==1)return 6;//*设定边界值1和40**//
else return sum(n-1)+k;
}
最简单的java代码肯定就是这个了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是应该是所有学java的新手看的第一个代码了。如果是零基础的新手朋友们可以来我们的java实验班试听,有免费的试听课程帮助学习java必备基础知识,有助教老师为零基础的人提供个人学习方案,学习完成后有考评团进行专业测试,帮助测评学员是否适合继续学习java,15天内免费帮助来报名体验实验班的新手快速入门java,更好的学习java!
import java.awt.*;
import java.applet.*;
public class CalculatorDemo extends Applet
{
TextField answerText;
Button pointButton,equalButton,plusButton;
Button minusButton,clearButton,multiButton,divisionButton;
Button[] b=new Button[10];
String currentOp,preOp;
String foreText,backText;
boolean isFloat=false;
public void init()
{
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
currentOp=new String("");
preOp=new String("");
foreText=new String("");
backText=new String("");
answerText=new TextField(8);
setBackground(Color.lightGray);
setForeground(Color.blue);
for(int i=9;i=0;i--)
{
b[i]=new Button(Integer.toString(i));
p2.add(b[i]);
}
pointButton=new Button(".");
equalButton=new Button("=");
equalButton.setForeground(Color.red);
clearButton=new Button("清除");
clearButton.setForeground(Color.red);
divisionButton=new Button("/");
divisionButton.setForeground(Color.red);
multiButton=new Button("*");
multiButton.setForeground(Color.red);
minusButton=new Button("-");
minusButton.setForeground(Color.red);
plusButton=new Button("+");
plusButton.setForeground(Color.red);
setLayout(new FlowLayout());
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(4,3));
p3.setLayout(new GridLayout(4,1));
p1.add(answerText);
p1.add(clearButton);
p2.add(pointButton);
p2.add(equalButton);
p3.add(plusButton);
p3.add(minusButton);
p3.add(multiButton);
p3.add(divisionButton);
add(p1);
add(p2);
add(p3);
}
public boolean action(Event e,Object o)
{
String s=new String("");
for(int i=0;i10;i++)
{
if(e.target==b[i]||e.target==pointButton)
{
if(e.target !=pointButton)
{
s=(String)o;
doForeText(s);
}
if((e.target==pointButton)(!isFloat))
{
isFloat=true;
s=(String)o;
if(foreText.equals(""))
{
foreText +="0.";
}
else
{
doForeText(s);
}
}
}
}
if(e.target==clearButton)
{
doClear();
}
if((e.target==clearButton)||(e.target==divisionButton)
||(e.target==plusButton)||(e.target==minusButton))
{
if(foreText !="")
{
currentOp=((String)o);
doOperator();
}
else
{
preOp=((String)o);
}
}
if(e.target==equalButton)
{
doOperator();
}
return true;
}
public void doOperator()
{
double dFore,dBack;
Double d;
if(preOp.equals(""))
{
backText=foreText;
foreText="";
answerText.setText(backText);
}
else
{
dFore=(new Double(foreText)).doubleValue();
dBack=(new Double(backText)).doubleValue();
foreText="";
backText=answerText.getText();
if(preOp.equals("+"))
{
d=new Double((dBack+dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("-"))
{
d=new Double((dBack-dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("*"))
{
d=new Double((dBack*dFore));
answerText.setText(d.toString());
backText=d.toString();
}
if(preOp.equals("/"))
{
if(dFore==0)
{
answerText.setText("除数不能为0");
return;
}
d=new Double((dBack/dFore));
answerText.setText(d.toString());
backText=d.toString();
}
}
preOp=currentOp;
}
public void doForeText(String s)
{
foreText +=s;
answerText.setText(foreText);
}
public void doBackText(String s)
{
backText=foreText;
foreText="";
answerText.setText(foreText);
}
public void doClear()
{
currentOp="";
preOp="";
foreText="";
backText="";
isFloat=false;
answerText.setText("");
}
}