You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public class FileTest1{
public static void main(String[] args){
File directory = new File("D:"+File.separator+"direc");
OutputStream output = null;
if (!directory.exists()) //若不存在“direc”这个文件夹,则创建
directory.mkdir();
try{ //创建10个txt文件
for (int i = 0; i < 10; i++){
File file = new File("D:"+File.separator+"direc"+File.separator+"hello"+ i +".txt");
if (!file.exists())
file.createNewFile();
}
}catch(IOException e){
e.printStackTrace();
}
String[] fileNames = directory.list(); //把directory中的文件名字保存在数组中
for (int i = 0; i < 10; i ++){ //向文件中写入
File hellos = new File("D:"+File.separator+"direc"+File.separator+"hello"+ i +".txt");
try{
FileOutputStream out = new FileOutputStream(hellos);
String str = new String("hello world "+ i);
byte[] bstr = str.getBytes();
out.write(bstr);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
public class TestCollection {
public static void main(String[] args){
ArrayList sheepList = new ArrayList();
sheepList.add(new sheep("喜羊羊"));
sheepList.add(new sheep("美羊羊"));
sheepList.add(new sheep("懒羊羊"));
sheepList.add(new sheep("沸羊羊"));
sheepList.add(new sheep("慢羊羊"));
sheepList.add(new sheep("暖羊羊"));
for (sheep s : sheepList)
System.out.println(s.name);
int n = 3; //删除羊的只数
int length = sheepList.size();
//从后面删除n只羊
for (int i = length -1; i >= length - n; i--) {
sheepList.remove(i);
}
//从前面删除n只羊
for (int i = 0; i < n; i++)
sheepList.remove(i);
Iterator it = sheepList.iterator();
//删除指定的羊
sheep goalSheep = new sheep("慢羊羊");
while (it.hasNext()){
sheep s = (sheep) it.next();
if (s.equals(goalSheep))
it.remove();
}
for (sheep s : sheepList)
System.out.println(s.name);
}
}
class animal{
public String name = "动物";
}
class sheep extends animal{
public String name;
private String head;
private String froot;
public sheep(String name){
this.name = name;
super.name = name;
}
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0:name.hashCode());
return result;
}
public boolean equals(Object obj){
if (this.name.equals(((sheep)obj).name)){
return true;
}else
return false;
}
}
3.多线程
package cn.edu.nuc;
public class Multithreading {
public static void main(String[] args){
SubThread s1 = new SubThread("a---");
SubThread s2 = new SubThread("b---");
SubThread1 s11= new SubThread1("c---");
SubThread1 s12 = new SubThread1("d---");
s1.start();
s2.start();
s1.run();
s2.run();
s11.run();
s12.run();
}
}
//subthread 单继承thread
class SubThread extends Thread{
private String name;
public SubThread(){
}
public SubThread(String name){
super(name);
}
/*public String name(){
return this.name;
}*/
public void run(){
for (int i = 0; i < 5; i ++){
System.out .println("这是同时进行的第 " + i + " 个"+this.getName());
}
}
}
class SubThread1 implements Runnable{
private String name;
public SubThread1(String name){
this.name = name;
}
public SubThread1(){
}
public void run(){
for (int i = 0; i < 5; i ++)
System.out.println("这是同时进行的第 "+ i + "个"+this.getName());
}
private String getName() {
// TODO Auto-generated method stub
return this.name;
}
}
The text was updated successfully, but these errors were encountered:
1.批量创建文件
package cn.edu.nuc;
import java.io.File;
import java.io.FileNotFoun-dException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileTest1{
public static void main(String[] args){
File directory = new File("D:"+File.separator+"direc");
OutputStream output = null;
}
2.删除羊
package cn.edu.nuc;
import java.util.ArrayList;
import java.util.Iterator;
public class TestCollection {
public static void main(String[] args){
ArrayList sheepList = new ArrayList();
sheepList.add(new sheep("喜羊羊"));
sheepList.add(new sheep("美羊羊"));
sheepList.add(new sheep("懒羊羊"));
sheepList.add(new sheep("沸羊羊"));
sheepList.add(new sheep("慢羊羊"));
sheepList.add(new sheep("暖羊羊"));
}
class animal{
public String name = "动物";
}
class sheep extends animal{
public String name;
}
3.多线程
package cn.edu.nuc;
public class Multithreading {
public static void main(String[] args){
SubThread s1 = new SubThread("a---");
SubThread s2 = new SubThread("b---");
}
//subthread 单继承thread
class SubThread extends Thread{
private String name;
public SubThread(){
}
class SubThread1 implements Runnable{
private String name;
public SubThread1(String name){
this.name = name;
}
public SubThread1(){
}
The text was updated successfully, but these errors were encountered: