Commit 25353a11 by lei.zhang

no message

parent b16f7cb9
Java 第一课
全心全意为门店服务
全心全意为门店服务
全心全意为门店服务
全心全意为门店服务全心全意为门店服务学习
学习java 第一课
\ No newline at end of file
package com.company;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Java Lesson1");
// MapPractice();
//SetPractice();
//ArrayPractice();
//LinkdListPractice();
//IOReadPractice();
//IOWritePractice();
//FileReaderPractice();
//FileWriterPractice();
ThreadPractice();
}
/* HashMap 使用举例*/
public static void MapPractice()
{
//集合HashMap
Map<String,Object> hmCollect = new HashMap();
hmCollect.put("Age",18); //插入数据
hmCollect.put("Name","test");
//获取数据
String name=hmCollect.get("Name").toString();
System.out.println("姓名:"+name);
//更新集合
hmCollect.replace("Name","Java");
name=hmCollect.get("Name").toString();
System.out.println("姓名:"+name);
//删除集合内数据
hmCollect.remove("Age");
//循环输出
hmCollect.forEach((k,v)->{
System.out.println("key:"+k+" value:"+v);
});
System.out.println("****** for循环输出 ****** ");
for(Map.Entry<String,Object> entry:hmCollect.entrySet()) {
System.out.println("item:" + entry.getKey() + " " + entry.getValue());
}
}
public static void SetPractice() {
Set hs = new HashSet();
String str = "freemud";
hs.add(str);
hs.add("shop");
if(hs.contains("freemud"))
{
System.out.println("已经存在");
}
Iterator it=hs.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
public static void ArrayPractice() {
ArrayList arrLis = new ArrayList();
for (int i = 0; i < 10; i++) {
arrLis.add(i);
}
ArrayList<People> arrliss = new ArrayList<>();
arrliss.add(new People("freemud", 18));
arrliss.add(new People("shop",19));
Iterator it=arrliss.iterator();
while(it.hasNext())
{
People peo=(People)it.next();
System.out.println(peo.name);
}
}
public static void LinkdListPractice(){
LinkedList list = new LinkedList();
//list.add()
LinkedList<String> lis=new LinkedList<String>();
lis.add("123");
lis.add("freemud");
Iterator it= lis.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
//字节流读取
public static void IOReadPractice()
{
FileInputStream fos=null;
try
{
fos = new FileInputStream("D:\\Private\\SpringBootDemo-master\\SpringBootDemo-master\\lessonOne\\out\\production\\lessonOne\\com\\company\\test.txt");
byte bytes[]=new byte[1024];
int n=0;
while((n=fos.read(bytes))!= -1){
String str = new String(bytes,0,n);
System.out.print(str);
}
}
catch(Exception ex)
{
System.out.println("读取文件出错:"+ex.getMessage());
}
finally
{
try
{
fos.close();
}
catch (Exception ex)
{
}
}
}
//字节流写入
public static void IOWritePractice()
{
FileOutputStream fos=null;
try
{
fos = new FileOutputStream("D:\\Private\\SpringBootDemo-master\\SpringBootDemo-master\\lessonOne\\out\\production\\lessonOne\\com\\company\\test.txt",true);
String str ="\r\n全心全意为门店服务";
byte bytes[] =str.getBytes();
fos.write(bytes);
}
catch(Exception ex)
{
System.out.println("写入出错:"+ex.getMessage());
}
finally {
try
{
fos.close();
}
catch (Exception ex)
{
}
}
}
//字符流读取
public static void FileReaderPractice()
{
FileReader fr=null;
try
{
fr = new FileReader("D:\\Private\\SpringBootDemo-master\\SpringBootDemo-master\\lessonOne\\out\\production\\lessonOne\\com\\company\\test.txt");
char chars[] = new char[1024];
int n=0;
while((n=fr.read(chars))!= -1)
{
System.out.println(chars);
}
}
catch (Exception ex)
{
System.out.println("读取出错:"+ex.getMessage());
}
finally {
try
{
fr.close();
}
catch (Exception ex)
{
}
}
}
//字符流写入
public static void FileWriterPractice()
{
FileWriter fw=null;
try
{
fw=new FileWriter("D:\\Private\\SpringBootDemo-master\\SpringBootDemo-master\\lessonOne\\out\\production\\lessonOne\\com\\company\\test.txt",true);
fw.write("\r\n学习java 第一课");
}
catch (Exception ex)
{
}
finally {
try {
fw.close();
}
catch (Exception ex)
{
}
}
}
public static void ThreadPractice()
{
ThreadTest thread1=new ThreadTest("开发");
thread1.start();
ThreadTest thread2=new ThreadTest("测试");
thread2.start();
}
}
package com.company;
public class People {
public People(String name,int age)
{
this.name=name;
this.age=age;
}
public String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
package com.company;
public class ThreadTest extends Thread{
public String workName;
public ThreadTest(String _workName)
{
this.workName=_workName;
}
@Override
public void run()
{
for(int i=0;i<6;i++)
{
System.out.println( "第"+i+"份工作:"+this.workName);
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
}
测试java
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment