您的位置:首页 > 编程语言 > Java开发

Java基础的一些简单实例

2005-09-19 08:27 513 查看
package swk.jee.ch1;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-21
* Time: 22:41:00
* 将用户输入的多个字符串保存到集合中,并消除重复值
*/
public class InHashSet {
public static void main(String[] args) {
Set set = new HashSet();
//set = new TreeSet();
for(int i=0;i<args.length;i++){
set.add(args[i]);
}
Enumeration e = Collections.enumeration(set);
for(;e.hasMoreElements();){
System.out.println(e.nextElement());
}
}
}
package swk.jee.ch1;
import java.util.LinkedList;
import java.util.Iterator;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-21
* Time: 22:15:21
* 演示LinkedList的用法
*/
public class InList {
public static void main(String[] args) {
LinkedList list = new LinkedList();
for(int i=0;i<10;i++){
list.add(new Integer(i));
}
list.addFirst(new Integer(-1));
list.addLast(new Integer(12));
list.removeFirst();
list.removeLast();

for(Iterator i= list.iterator();i.hasNext();){
System.out.println(i.next());
}
}
}

package swk.jee.ch1;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-21
* Time: 22:43:57
* 演示HashMap及TreeMap的使用
*/
public class InMap {
public static void main(String[] args) {
Map map = new HashMap();
//map = new TreeMap();
map.put("Lucy",new Double(21.100));
map.put("Jack",new Double(123.1));
map.put("Lily",new Double(1221.01));
map.put("Edison",new Double(3455.11));
//System.out.println(map);
/* 获得所有键 */
Set keySet =map.keySet();
System.out.println("键:");
for(Iterator i=keySet.iterator();i.hasNext();){
System.out.print(i.next()+" ");
}
System.out.println("");
/* 获得所有的值 */
Collection values = map.values();
System.out.println("值:");
Iterator i2 = values.iterator();
while(i2.hasNext()){
System.out.print(i2.next()+" ");
}
System.out.println("");
/* 获得指定键的值 */
System.out.println(map.get("Lily"));
}
}

package swk.jee.ch1;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-21
* Time: 22:41:00
* 将用户输入的多个字符串保存到集合中,并消除重复值
*/
public class InSet {
public static void main(String[] args) {
Set set = new HashSet();
//set = new TreeSet();
for(int i=0;i<args.length;i++){
set.add(args[i]);
}
Enumeration e = Collections.enumeration(set);
for(;e.hasMoreElements();){
System.out.println(e.nextElement());
}
}
}
package swk.jee.ch2;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-22
* Time: 20:54:50
* 文件拷贝类,实现把指定的源文件拷贝成一新文件。
*/
public class FileCopier {
private File source; //源文件
public FileCopier(File source){
this.source = source; //指定源文件
}
/**
* 将现在文件复制成一份新的文件
* @param target 生成的新文件
* */
public void copyTo(File target) throws IOException {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(target);
BufferedInputStream bis = new BufferedInputStream(fis); //带缓冲的读
BufferedOutputStream bos = new BufferedOutputStream(fos); //带缓冲的写
int cacheSize = 1024; // 初始cache大小
while(bis.available()!=0){
/*
//简单的读与写
byte bit = (byte)bis.read();
fos.write(bit); //如果使用bos来写,为什么会写的不正确?
*/
if(cacheSize<bis.available())
cacheSize = bis.available();
byte[] cache = new byte[cacheSize];
bis.read(cache);
bos.write(cache);
}
fis.close();
fos.close();
bis.close();
bos.close();
}
/* 使用 Reader 和 Writer来实现 */
public void copyToUseCharFlow(File target) throws IOException{
//
}
public static void main(String[] args) {
if(args.length!=2){
System.out.println("请按如下格式运行程序: java FileCopier 源文件 目的文件");
return;
}
File source = new File(args[0]);
File target = new File(args[1]);
if(!(source.exists()&&source.isFile())){
System.out.println("源文件不存在, 请重新指定!");
return;
}
FileCopier fc = new FileCopier(source);
try {
fc.copyTo(target);
System.out.println("复制成功!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

}

package swk.jee.ch2;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-22
* Time: 20:17:03
* To change this template use File | Settings | File Templates.
*/
public class FileDetector {
private String[] files;
public FileDetector(String[] files){
this.files = files;
}
public void detect() throws NoFileSpecifyException,IOException {
if(files.length==0){
throw new NoFileSpecifyException("请指定文件!");
}
for(int i=0;i<files.length;i++){
File file = new File(files[i]);
if(file.isFile()&&file.exists()){
show(file);
} else {
System.out.println(file.getName()+"不存在或为目录!");
}
}
}
private void show(File file) throws IOException{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String msg;
System.out.println(file.getName()+"的内容为:");
while((msg=br.readLine())!=null){
System.out.println(msg);
}
fr.close();
br.close();
}
/*
public static void showContents(File file){
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String msg;
while((msg=br.readLine())!=null){
System.out.println(msg);
}
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch( IOException ioe){
}
} */
public static void main(String[] args) {
FileDetector fd = new FileDetector(args);
try {
fd.detect();
} catch (NoFileSpecifyException e) {
System.out.println("请指定要检测的文件列表!");
} catch (IOException e) {
System.out.println("发生异常错误,程序中止!");
}
/*
if(args.length==0){
System.out.println("请指定参数!");
return;
}
File file;
for(int i=0;i<args.length;i++){
file = new File(args[i]);
if(file.exists()&&file.isFile()){
System.out.println("第"+i+"个文件的内容:");
showContents(file);
}
System.out.println("");
}
*/
}
}
class NoFileSpecifyException extends Exception{
private String msg = "必须指定文件";
public NoFileSpecifyException(){
}
public NoFileSpecifyException(String msg){
super(msg);
}
}

package swk.jee.ch2;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-23
* Time: 13:34:13
* 文件读
*/
public class ReadFromFileUseInputStream {
public static void main(String[] args) {
File file = new File("d:/abc.txt");
try {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
char ch;
while(bis.available()!=0){
ch = (char)bis.read();
System.out.print(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}

package swk.jee.ch2;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-22
* Time: 19:13:00
* To change this template use File | Settings | File Templates.
*/
public class TestRead {
public static void main(String[] args) {
DataInputStream dis = new DataInputStream(System.in);
BufferedInputStream bis = new BufferedInputStream(dis);
/* 将从键盘输入的内容放入一cache, 再从cache中读。 */
try {
int a = 0;
while((char)a!='x'){
a= dis.read();
//bis.skip(2);
System.out.println(dis.available());
System.out.println((char)a);
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}

package swk.jee.ch2;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-22
* Time: 19:35:43
* To change this template use File | Settings | File Templates.
*/
public class tryFileInputStream {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fout = null;
try {
fis= new FileInputStream(new File("D:/test.txt"));
BufferedInputStream bis = new BufferedInputStream(fis);
fout = new FileOutputStream(new File("D:/test2.txt"));
BufferedOutputStream bos = new BufferedOutputStream(fout);
char ch;
//byte[] chs = new byte[512];
while(fis.available()!=0){
// bis.read(chs);
ch = (char)bis.read();
//bos.write(chs);
bos.write(ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException ioe){
}
}
}

package swk.jee.ch2;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-23
* Time: 13:48:04
* 接收用户的输入,如果输入为end,则停止接收
*/
public class TryReader {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str;
try{
while(!(str=br.readLine()).equals("end")){
System.out.println(str);
}
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}

package swk.jee.ch2;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: 凌云一笑
* Date: 2005-8-23
* Time: 13:39:54
* 写文件
*/
public class WriteFileUseOutputStream {
public static void main(String[] args) {
File file = new File("D:/abc.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
String str = "ajfkadfjdfk ds f dsf 12121";
int index =0;
while(index<(str.length()-1)){
dos.writeChar(str.charAt(index));
index++;
}
dos.close();
bos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException ioe){
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: