JAVA基础 业 ,精于勤 荒于嬉.
- JAVA基础 1.JAVA开发环境配置
-
发表日期:2015-07-07 18:23:43 | 来源: | 分类:JAVA基础
-
JAVA开发环境配置,安装成功JVAV JDK后,一般都需要配置系统环境变量,否则在DOS下的命令会找不到文件报错。
这里要特别注意:“如果你的配置和以下相同并且无误,还是会报错如标题,那么你重启一下电脑就OK了!我在2003下既是此种情况,即可解决!而大多数软件配置环境变量是不需要重启的。”如下:
javac不是内部或外部命令
配置:
新建系统环境变量
名称:JAVA_HOME 值为: E:\Program Files\Javajdk1.6.0_11(这里修改为你的安装路径)
名称:PATH(一般系统都有,没有你就新建个) 值为:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
名称:CLASSPATH值为:%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
《这里需要注意,一个环境变量有一个名称,但可以设置多个值,每个值以分号“;”分开。切勿连在一块了》
这样设置就OK了!尽量重启一下电脑然后CMD下 输入javac ,不报错就完成了!
- JAVA基础 2.java数据类型
-
发表日期:2015-07-07 18:23:43 | 来源: | 分类:JAVA基础
-
示例1
public class NotePad { public static void main(String[] args) { int i = 10; float f = 20.66f; double d = 30.654; String s = "abc"; float DtoF = (float) d;//转型 /** * @describe 整型(Integer) */ int min = Integer.MIN_VALUE;//整型最小值 int max = Integer.MAX_VALUE;//整型最大值 System.out.println(min);//-2147483648 System.out.print(max);//2147483647 System.out.println("print和println的区别第一个打印字符不换行,第二个打印完换一行."); /** * @describe 字符类型(char) * 只能存两个字节的数据,所以只能存一个字母或一个汉字 * char 范围是0-255 * 字符数据只能用单引号括起来不能用双引号括起来,也就是'数据内容' */ char c1 = 'a'; //char c1 = "a";此代码是错的,char类型的字符数据只能用单引号不能用双引号'数据内容',用双引号应该是String类型 char c2 = 97; System.out.println(c1);//a System.out.println(c2);//a /** * @describe 浮点型(float) */ float f1 = (float) 0.5655;//要么强制转型 float f2 = 0.5655f;//要么浮点数后加 f代表是浮点数 System.out.println(f1);//0.5655 System.out.println(f2);//0.5655 /** * @describe 双精度型(double) */ double d1 = 3420.5655; double d2 = 3420.5655d; System.out.println(d1);//3420.5655 System.out.println(d2);//3420.5655 /** * @describe 布尔型(boolean) * * boolean b3 = 1;//此代码是错的,java是强类型语言,boolean类型就有两个值true或者false, * 不像PHP等其他语言是弱类型语言1也表示true,0表示false,甚至只要是非0或null都表示true */ boolean b1 = true; boolean b2 = false; System.out.println(b1);//true System.out.println(b2);//false /** * @describe 类型转换 * 任何类型都可以像String类型转换,比如数字123 可以转换为字符串 "123" * 但是不一定任何类型的String都可以转换为数字 比如 "abc" 就不可能转换为数字,除非是"数字字符串"才可以转换。 */ //int i = 1.23;//此代码是错误的、因为int是整型只能表示整数 如:int i = 1; //int i = 1.23f;//此代码是错误的、因为1.23f就表示是浮点数了,它怎么能和整型匹配呢,这不叫转型 int i = (int)1.23;//将浮点数1.23强制转换为整型,那么结果当然是1咯 System.out.println(i);//1 //其它转型亦如此,不一一列举 } }
- JAVA基础 3.数组
-
发表日期:2015-02-25 21:29:56 | 来源: | 分类:JAVA基础
-
示例1
public class ArrayDemo { public static void main(String[] args) { int arr1[] = null;//声明数组 arr1 = new int[9];//为数组开辟9个空间的 大小 //或 int[] arr2 = null; arr2 = new int[9]; //或 int[] arr3 = new int[9]; //或 int arr4[] = new int[9]; arr4[0] = 1;//数组的下标第一位是从0开始的,开辟9个空间,那么最大到8,即0-8 arr4[1] = 2; arr4[2] = 3; arr4[3] = 4; //..... int arr[] = {15,68,84,10,9,20,64,31,3,75,99,21};//数组静态初始化 System.out.println("数组长度"+arr.length);//数组长度12 getMinMax(arr); } public static void getMinMax(int[] arr){ int max = 0; int min = 0; max = min = arr[0]; for (int i = 0; i < arr.length; i++) { if (arr[i]<min) { min = arr[i]; } if (arr[i]>max) { max = arr[i]; } } System.out.println("最小值:"+min);//3 System.out.println("最大值:"+max);//99 } }
示例2
import java.util.Arrays; public class ArrayDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int temp[] = {1,5,9,55,65,62,37,7}; Arrays.sort(temp); System.out.println(Arrays.toString(temp));//[1, 5, 7, 9, 37, 55, 62, 65] int point =Arrays.binarySearch(temp, 7);//2 System.out.println(point); Arrays.fill(temp, 7); System.out.println(Arrays.toString(temp));//[7, 7, 7, 7, 7, 7, 7, 7] } }
- JAVA基础 4.Date
-
发表日期:2021-06-30 19:37:09 | 来源: | 分类:JAVA基础
-
示例1
import java.util.Date; public class DateDemo01 extends Date { /** * */ private static final long serialVersionUID = 1L; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Date date = new Date(); System.out.println(date);//Tue Sep 10 15:43:13 CST 2013 } }
示例2
import java.util.Calendar; import java.util.GregorianCalendar; public class DateDemo02 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Calendar calendar = new GregorianCalendar(); System.out.println("year:"+calendar.get(Calendar.YEAR));//year:2013 System.out.println("month:"+(calendar.get(Calendar.MONTH)+1));//month:9 System.out.println("day:"+calendar.get(Calendar.DAY_OF_MONTH));//day:10 System.out.println("hour:"+calendar.get(Calendar.HOUR_OF_DAY));//hour:15 System.out.println("minute:"+calendar.get(Calendar.MINUTE));//minute:50 System.out.println("second:"+calendar.get(Calendar.SECOND));//second:30 System.out.println("millksecond:"+calendar.get(Calendar.MILLISECOND));//Millisecond:862 } }
示例3
import java.text.DateFormat; import java.text.FieldPosition; import java.text.ParsePosition; import java.util.Date; public abstract class DateDemo03 extends DateFormat { public static void main(String[] args) { // TODO Auto-generated method stub DateFormat dateFormat1 = DateFormat.getDateInstance(); DateFormat dateFormat2 = DateFormat.getDateTimeInstance(); System.out.println("Date:"+dateFormat1.format(new Date()));//Date:2013-9-10 System.out.println("DateTime:"+dateFormat2.format(new Date()));//DateTime:2013-9-10 16:01:54 } }
示例4
import java.text.DateFormat; import java.text.FieldPosition; import java.text.ParsePosition; import java.util.Date; import java.util.Locale; public class DateDemo04 extends DateFormat { /** * */ private static final long serialVersionUID = 1L; @Override public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition fieldPosition) { // TODO Auto-generated method stub return null; } @Override public Date parse(String source, ParsePosition pos) { // TODO Auto-generated method stub return null; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DateFormat dateFormat1 = DateFormat.getDateInstance(DateFormat.YEAR_FIELD,new Locale("zh","CN")); DateFormat dateFormat2 = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN")); System.out.println("Date:"+dateFormat1.format(new Date()));//Date:2013年9月10日 System.out.println("DateTime:"+dateFormat2.format(new Date()));//DateTime:2013年9月10日 下午04时05分59秒 CST } }
示例5
//丢了
示例6
import java.text.SimpleDateFormat; import java.util.Date; import com.sun.org.apache.bcel.internal.generic.NEW; public class DateDemo06 extends SimpleDateFormat { /** * */ private static final long serialVersionUID = 1L; /** * @param args */ public static void main(String[] args) { //new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()) // TODO Auto-generated method stub String strDate = "2013-9-10 16:07:28.345"; String par1 = "yyyy-MM-dd HH:mm:ss.SSS"; String par2 = "yyyy年MM月dd日 HH使mm分ss秒SSS毫秒"; SimpleDateFormat sFormat1 = new SimpleDateFormat(par1); SimpleDateFormat sFormat2 = new SimpleDateFormat(par2); Date date = null; try { date = sFormat1.parse(strDate); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } System.out.println(date); System.out.println(sFormat2); System.out.println(sFormat2.format(date)); System.out.println(sFormat2.format(new Date())); System.out.println(new SimpleDateFormat("yyyy年MM月dd日 HH使mm分ss秒SSS毫秒").format(new Date())); } }
示例7
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo07 { public static void main(String[] args) throws ParseException { String date = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()); //System.out.println(date); String string = "2015年11月02日 01:37:39"; String par_res = "yyyy年MM月dd日 HH:mm:ss"; SimpleDateFormat resFormat = new SimpleDateFormat(par_res); Date resDateTime = resFormat.parse(string); String par_data = "yyyy-MM-dd"; SimpleDateFormat dataFormat = new SimpleDateFormat(par_data); String par_time = "HH:mm"; SimpleDateFormat timeFormat = new SimpleDateFormat(par_time); System.out.println(dataFormat.format(resDateTime)); System.out.println(timeFormat.format(resDateTime)); } }
- JAVA基础 5.String 和 StringBuffer类常用方法
-
发表日期:2015-07-07 23:16:51 | 来源: | 分类:JAVA基础
-
示例1
public class StringDemo { public static void main(String[] args) { // TODO 自动生成的方法存根 String name1 = "eniac"; String name2 = new String("eniac"); String name3 = "eniac"; String name4 = new String("eniac"); System.out.println(name1);//eniac System.out.println(name2);//eniac System.out.println(name3);//eniac System.out.println(name4);//eniac //那么比较一下呢? System.out.println(name1==name2);//false System.out.println(name1==name3);//true System.out.println(name2==name4);//false /* 值都是eniac,为什么会不同呢?这就要内存分析了, * JAVA这个语言 A==B 和其他语言不同,它不是比较内容的值是否相同,而是比较内存地址是否相同 * new String() 会每次都开辟一个新空间,所以内存地址会不同, * 而 String name1 = "eniac",会开辟一次 而 String name3 = "eniac" 会引用上一次name1的内存地址 * 所以,从性能而言,尽量使用 String name3 = xxx 的 */ //如果一定要比较值是否相同,我们不在意内存地址是否相同的话,可以这样 System.out.println(name1.equals(name2));//true //字符串换成字符数组 String str1 = "canquick"; char[] Cstr1 = str1.toCharArray(); System.out.println(Cstr1);//canquick for (int i = 0; i < Cstr1.length; i++) { char c = Cstr1[i]; System.out.println(c);//c、a、n、q、u、i、c、k } //字符数组换成字符串 String str2 = new String(Cstr1); String str3 = new String(Cstr1,3,5); System.out.println(str2);//canquick System.out.println(str3);//quick //从字符串中取出指定位置的字符 char str4 = str1.charAt(3); System.out.println(str4);//q //字符串与byte数组转换 byte[] byte1 = str1.getBytes(); System.out.println(byte1); System.out.println(new String(byte1));//canquick System.out.println(new String(byte1,3,5));//canquick //取得一个字符串的长度 System.out.println(str1.length());//8 //查找指定字符串是否存在 System.out.println(str1.indexOf("i"));//5 System.out.println(str1.indexOf("i",3));//从第四个位置开始查找 //去掉空格 System.out.println(" hello !".trim());//hello ! //字符截取 System.out.println(str1.substring(1, 3));//an //拆分字符串 String str5[] = str1.split("i"); for (String string : str5) { System.out.println(string);//canqu、ck } //大小写转换 System.out.println(str1.toUpperCase());//CANQUICK (toLowerCase:转小写) //判断字符串是否以指定字符开头或结尾 System.out.println(str1.startsWith("can"));//true System.out.println(str1.endsWith("ck"));//true //不区分大小写比较 System.out.println(str1.equalsIgnoreCase("CANQUICK"));//true //字符串替换 System.out.println(str1.replaceAll("quick", "like"));//canlike } }
示例2
public class StringBufferDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer buffer = new StringBuffer(); buffer.append("Hello");//Hello buffer.append("world").append("!!!");//Helloworld!!!//字符串插入 buffer.insert(5," ");//Hello world!!! buffer.reverse();//!!!dlrow olleH//字符串反转 buffer.reverse();//Hello world!!! buffer.replace(6, 11, "ENIAC");//Hello ENIAC!!!//字符串替换 System.out.println(buffer.substring(6, 11));//ENIAC 字符串截取 buffer.delete(6, 11);//Hello !!!//字符串删除 System.out.println(buffer.indexOf("llo"));//2 //查找字符串(-1为无) System.out.println(buffer); } }
- JAVA基础 6.Math类
-
发表日期:2015-07-07 21:12:26 | 来源: | 分类:JAVA基础
-
示例1
import java.util.Random; public class MathDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("求平方根:"+Math.sqrt(9.0));//求平方根:3.0 System.out.println("求最大值:"+Math.max(57, 35));//求最大值:57 System.out.println("求最小值:"+Math.min(57, 35));//求最小值:35 System.out.println("2的3次方:"+Math.pow(2, 3));//2的3次方:8.0 System.out.println("四舍五入:"+Math.round(3.14));//四舍五入:3 Random random = new Random(); for (int i = 0; i < 10; i++) { System.out.println(random.nextInt(100));//小于100的随机数 } } }
- JAVA基础 7.Cloneable
-
发表日期:2021-06-30 19:49:04 | 来源: | 分类:JAVA基础
-
示例1
class Person implements Cloneable{ private String name; public Person(String name) { this.name = name; } // TODO Auto-generated constructor stub public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public Object clone() throws CloneNotSupportedException{ return super.clone(); } } public class CloneDemo01 { /** * @param args * @throws CloneNotSupportedException */ public static void main(String[] args) throws CloneNotSupportedException { // TODO Auto-generated method stub Person person1 = new Person("张三"); Person person2 = (Person)person1.clone(); person2.setName("李四"); System.out.println(person1); System.out.println(person2); } }
- JAVA基础 8.File 文件
-
发表日期:2021-06-30 20:01:30 | 来源: | 分类:JAVA基础
-
示例1
import java.io.File; import java.io.IOException; public class FileDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("d:"+File.separator+"test.txt"); System.out.println(File.separator);// \ System.out.println(File.separatorChar);// \ System.out.println(File.pathSeparator);// ; System.out.println(File.pathSeparatorChar);// ; try { file.createNewFile();//创建文件 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(file.exists()){//判断文件是否存在 //file.delete();//产出文件 } } }
示例2
import java.io.File; public class FileDemo02 { private static int ListModel = 0;//0:列文件夹和文件、1:只列出文件夹、2:只列出文件 public static void main(String[] args) { // TODO Auto-generated method stub ListModel = 1; File file = new File("d:"+File.separator); listFile(file); } private static void listFile(File file) { // TODO Auto-generated method stub if (file!=null) { if (file.isDirectory()) { if (ListModel!=2) { System.out.println(file);//列出文件夹 } File str[] = file.listFiles(); if (str!=null) { for (int i = 0; i < str.length; i++) { listFile(str[i]); } } }else { if (ListModel!=1) { System.out.println(file);//列出文件夹 } } } } }
示例3
package File; import java.io.File; import java.io.IOException; public class FileDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("D:"+File.separator+"test.txt"); if(!file.exists()){ try { System.out.println("文件不存在,创建!"); file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { System.out.println("文件存在,不创建!"); } File dir = new File("D:"+File.separator+"dir"); if(!dir.exists()){ System.out.println("文件夹不存在,创建!"); dir.mkdir(); } if(file.exists()){ file.delete(); System.out.println("删除文件!"); } File disk = new File("D:"+File.separator); String[] lists = disk.list();//只列出名称 for (int i = 0; i < lists.length; i++) { System.out.println(lists[i]); } File[] lists2 = disk.listFiles();//列出完整路径 for (int i = 0; i < lists2.length; i++) { if (isDir(lists2[i].toString())) { System.out.println(lists2[i]+"是目录"); }else { System.out.println(lists2[i]+"不是目录"); } } } public static boolean isDir(String path){ return new File(path).isDirectory(); } }
- JAVA基础 9.FileReader和FileWriter
-
发表日期:2022-08-05 15:57:36 | 来源: | 分类:JAVA基础
-
示例1
package File; import java.io.File; import java.io.FileReader; import java.io.IOException; public class FileReaderDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:"+File.separator+"FileWriterDemo.txt"); FileReader fileReader = new FileReader(file); char[] c = new char[1024]; int length = fileReader.read(c); //int length = (int)file.length();其实获取文件大小本该没有错,可是却错了,后面是方格格占位符,为什么呢?如果是汉字会占用2个字节而这里却是以字符读取的。多了一倍 System.out.println(new String(c , 0, length)); fileReader.close(); } }
示例2
package File; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:"+File.separator+"FileWriterDemo.txt"); FileWriter fWriter = new FileWriter(file,false); String string = "踩踩踩踩踩"; fWriter.write(string); fWriter.close(); } }
- JAVA基础 10.RandomAccessFile
-
发表日期:2022-08-05 16:09:05 | 来源: | 分类:JAVA基础
-
示例1
package File; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; public class RandomAccessFileDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:" + File.separator + "RandomAccessFile.txt"); //两种写法均可 //RandomAccessFile rFile = new RandomAccessFile("D:"+File.separator+"test.txt", "rw"); RandomAccessFile rFile = new RandomAccessFile(file, "rw"); String string = "abcdefg我去这是什么情况?"; ; rFile.write(string.getBytes());//写入中文会乱码 rFile.close(); write(); read(); } private static void read() { // TODO Auto-generated method stub File file = new File("d:" + File.separator + "123.txt"); RandomAccessFile rFile = null; try { rFile = new RandomAccessFile(file, "r"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { byte c[] = new byte[(int) rFile.length()]; rFile.read(c); System.out.println(new String(c)); } catch (IOException e) { e.printStackTrace(); } } public static void write() { File file = new File("d:" + File.separator + "123.txt"); RandomAccessFile rFile = null; try { rFile = new RandomAccessFile(file, "rw"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String name = "zhangsan"; int age = 30; try { rFile.writeInt(age); // rFile.writeBytes(name); rFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
- JAVA基础 11.FileInputStream和FileOutputStream
-
发表日期:2021-06-30 19:50:39 | 来源: | 分类:JAVA基础
-
示例1
package stream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:" + File.separator + "123.txt"); FileInputStream inputStream = new FileInputStream(file); /**/ byte[] b = new byte[1024]; int length = inputStream.read(b);//获取文件内容长度 System.out.println(new String(b)); int length2 = (int) file.length();//第二中方法(获取文件大小[字节数]) System.out.println(new String(b, 0, length2)); /* System.out.println(file.length()); byte[] b2 = new byte[(int) file.length()]; int len = 0; int temp = 0; while ((temp=inputStream.read())!=-1) { b2[len] = (byte) temp; len++; } System.out.println(new String(b2,0,len)); */ inputStream.close(); } }
示例2
package stream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class OutputStreamDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:"+File.separator+"OutputStreamDemo.txt"); FileOutputStream fOutputStream = new FileOutputStream(file,true); String string ="true-追加写入模式"; fOutputStream.write(string.getBytes()); fOutputStream.close(); } }
- JAVA基础 12.InputStreamReader和OutputStreamWriter
-
发表日期:2022-08-05 16:13:02 | 来源: | 分类:JAVA基础
-
示例1
package stream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:"+File.separator+"123.txt"); FileInputStream stream = new FileInputStream(file); InputStreamReader reader = new InputStreamReader(stream); char c[] = new char[1024]; int length = reader.read(c); System.out.println(new String(c,0,length)); stream.close(); reader.close(); } }
示例2
package stream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class OutputStreamWriterDemo { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("D:"+File.separator+"123.txt"); FileOutputStream stream = new FileOutputStream(file); OutputStreamWriter writer = new OutputStreamWriter(stream); writer.write("测试一下3。"); writer.close(); stream.close(); } }
- JAVA基础 13.BufferedReader
-
发表日期:2022-08-05 16:17:46 | 来源: | 分类:JAVA基础
-
示例1
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ReaderDemo01 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("d:" + File.separator + "OutputStreamDemo.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } fr.close(); br.close(); /* FileReader fReader = null; try { fReader = new FileReader(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } BufferedReader bReader = new BufferedReader(fReader); try { System.out.print(bReader.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ } }
示例2
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; public class ReaderDemo02 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub InputStream is = System.in;//字节流 InputStreamReader isr = new InputStreamReader(is);//转换为字符流 BufferedReader br = new BufferedReader(isr); System.out.println("请输入内容"); String str = null; while ((str = br.readLine())!=null) { if ("exit".equals(str)) { break; } System.out.println(str); } is.close(); isr.close(); br.close(); System.out.println("bye!"); /* BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); String string = null; System.out.println("请输入内容"); try { string = bReader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("输入内容:"+string); */ } }
示例3
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class ExecDemo01 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub int i = 0 ; int j = 0 ; BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); String string = null; System.out.println("请输入第一个数字:"); string = bReader.readLine(); i = Integer.parseInt(string); System.out.println("请输入第二个数字:"); string = bReader.readLine(); j = Integer.parseInt(string); System.out.println(i+"+"+j+"="+(i+j)); } }
- JAVA基础 14.Scanner
-
发表日期:2022-08-05 16:19:27 | 来源: | 分类:JAVA基础
-
示例1
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerDemo01 { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub File file = new File("d:"+File.separator+"test.txt"); Scanner scanner = new Scanner(file); /* String string = scanner.next(); System.out.println(string); */ while (scanner.hasNext()) { String string = (String) scanner.next(); System.out.println(string); } } }
- JAVA基础 15.DataOutputStream
-
发表日期:2022-08-05 16:20:31 | 来源: | 分类:JAVA基础
-
示例1
import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class OutputDemo01 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("d:"+File.separator+"test.txt"); FileOutputStream os = new FileOutputStream(file); DataOutputStream dos = new DataOutputStream(os); int arg = 123; dos.writeInt(arg); } }
- JAVA基础 16.Thead 多线程
-
发表日期:2021-06-30 19:58:59 | 来源: | 分类:JAVA基础
-
示例1
class MyThead extends Thread { private int ticket =10; private String name; public MyThead(String name){ this.name = name; } public void run(){ for (int i = 0; i < 10; i++) { if (this.ticket>0) { System.out.println(name+":"+i+" ticket:"+ticket--); } } } } public class TheadDemo01 extends Thread { public static void main(String[] args) { // TODO Auto-generated method stub MyThead m1 = new MyThead("A"); MyThead m2 = new MyThead("B"); /* m1.run(); m2.run(); A:0 A:1 A:2 A:3 A:4 A:5 A:6 A:7 A:8 A:9 B:0 B:1 B:2 B:3 B:4 B:5 B:6 B:7 B:8 B:9 run() 不会实现多线程 */ m1.start(); m2.start(); } }
示例2
class MyThead2 implements Runnable{ private String name; private int ticket =10; public MyThead2(String name){ this.name = name; } public void run(){ for (int i = 0; i < 10; i++) { if (ticket>0) { System.out.println(name+":"+i+" ticket:"+ticket--); } } } public void start() { // TODO Auto-generated method stub } } public class TheadDemo02 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyThead2 m1 = new MyThead2("A"); MyThead2 m2 = new MyThead2("B"); Thread t1 = new Thread(m1); Thread t2 = new Thread(m2); t1.start(); t2.start(); System.out.println(t2.getName());//setName() ...设置线程名称 } }
示例3
class MyThead3 implements Runnable{ private int ticket =10; public MyThead3(){ } public void run(){ for (int i = 0; i < 10; i++) { if (this.ticket>0) { System.out.println(i+" "+Thread.currentThread().getName()+" ticket:"+ticket--); } } } } public class TheadDemo03 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyThead3 mt = new MyThead3(); new Thread(mt,"A").start(); new Thread(mt,"B").start(); new Thread(mt,"C").start(); } }
示例4
class MyThread4 implements Runnable{ private int ticket = 10; public MyThread4(){ } public void run(){ for (int i = 0; i < 10; i++) { //System.out.println(name+":"+i); if (this.ticket>0) { System.out.println(Thread.currentThread().getName()+":"+i+" ticket:"+ticket--); } } } } public class TheadDemo04 { public static void main(String[] args) { // TODO 自动生成的方法存根 MyThread4 mt1 = new MyThread4(); new Thread(mt1).run(); new Thread(mt1).run(); } }
- JAVA基础 17.TimerTask
-
发表日期:2022-08-05 15:20:57 | 来源: | 分类:JAVA基础
-
示例1
import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; public class MyTask extends TimerTask {//必须继承TimerTask public void run(){ System.out.println(new SimpleDateFormat("yyyy年MM月dd日 HH点mm分ss秒").format(new Date())); } }
示例2
import java.util.Timer; public class Task { public static void main(String[] args) { Timer timer = new Timer(); MyTask myTask = new MyTask(); timer.schedule(myTask, 1000, 2000); } }
- JAVA基础 18.zip
-
发表日期:2022-08-05 16:22:18 | 来源: | 分类:JAVA基础
-
示例1
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipDemo01 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("d:"+File.separator+"test.txt"); File zipFile = new File("d:"+File.separator+"test.zip"); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); zos.setComment("这里是注释"); ZipEntry zEntry = new ZipEntry(file.getName()); zos.putNextEntry(zEntry); int temp = 0; while ((temp=fis.read())!=-1) { zos.write(temp); } zos.finish() ; zos.close(); } private static Object Charset(String string) { // TODO Auto-generated method stub return null; } }
示例2
/** * Created by Administrator on 2016/4/5. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class ZipUtils { public static void zip(ArrayList<String> src, String dest) throws IOException { ZipOutputStream out = null; try { File outFile = new File(dest);// 源文件或者目录 out = new ZipOutputStream(new FileOutputStream(outFile)); for (int i = 0; i < src.size(); i++) { File fileOrDirectory = new File(src.get(i));// 压缩文件路径 zipFileOrDirectory(out, fileOrDirectory, ""); } } catch (IOException ex) { ex.printStackTrace(); } finally { // 关闭输出流 if (out != null) { try { out.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } public static void zip(String src, String dest) throws IOException { // 提供了一个数据项压缩成一个ZIP归档输出流 ZipOutputStream out = null; try { File outFile = new File(dest);// 源文件或者目录 File fileOrDirectory = new File(src);// 压缩文件路径 out = new ZipOutputStream(new FileOutputStream(outFile)); // 如果此文件是一个文件,否则为false。 if (fileOrDirectory.isFile()) { zipFileOrDirectory(out, fileOrDirectory, ""); } else {// 返回一个文件或空阵列。 File[] entries = fileOrDirectory.listFiles(); for (int i = 0; i < entries.length; i++) { // 递归压缩,更新curPaths zipFileOrDirectory(out, entries[i], ""); } } } catch (IOException ex) { ex.printStackTrace(); } finally { // 关闭输出流 if (out != null) { try { out.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } private static void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory, String curPath) throws IOException { // 从文件中读取字节的输入流 FileInputStream in = null; try { // 如果此文件是一个目录,否则返回false。 if (!fileOrDirectory.isDirectory()) { // 压缩文件 byte[] buffer = new byte[4096]; int bytes_read; in = new FileInputStream(fileOrDirectory); // 实例代表一个条目内的ZIP归档 ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName()); // 条目的信息写入底层流 out.putNextEntry(entry); while ((bytes_read = in.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); } out.closeEntry(); } else { // 压缩目录 File[] entries = fileOrDirectory.listFiles(); for (int i = 0; i < entries.length; i++) { // 递归压缩,更新curPaths zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/"); } } } catch (IOException ex) { ex.printStackTrace(); // throw ex; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } @SuppressWarnings("unchecked") public static void unzip(String zipFileName, String outputDirectory) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(zipFileName); Enumeration e = zipFile.entries(); ZipEntry zipEntry = null; File dest = new File(outputDirectory); dest.mkdirs(); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); String entryName = zipEntry.getName(); InputStream in = null; FileOutputStream out = null; try { if (zipEntry.isDirectory()) { String name = zipEntry.getName(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); f.mkdirs(); } else { int index = entryName.lastIndexOf("\\"); if (index != -1) { File df = new File(outputDirectory + File.separator + entryName.substring(0, index)); df.mkdirs(); } index = entryName.lastIndexOf("/"); if (index != -1) { File df = new File(outputDirectory + File.separator + entryName.substring(0, index)); df.mkdirs(); } File f = new File(outputDirectory + File.separator + zipEntry.getName()); // f.createNewFile(); in = zipFile.getInputStream(zipEntry); out = new FileOutputStream(f); int c; byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.flush(); } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("解压失败:" + ex.toString()); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } if (out != null) { try { out.close(); } catch (IOException ex) { } } } } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("解压失败:" + ex.toString()); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException ex) { } } } } } /* * Activity调用 package com.comc; import java.io.IOException; import * com.zipUtil.ZipUtil; import android.app.Activity; import android.os.Bundle; * public class IZipActivity extends Activity { * * * @Override public void onCreate(Bundle savedInstanceState) { * super.onCreate(savedInstanceState); setContentView(R.layout.main); try { * ZipUtil.zip("/data/data/com.comc/databases", * "/data/data/com.comc/databases.zip"); * ZipUtil.unzip("/data/data/com.comc/databases.zip", * "/data/data/com.comc/databases"); } catch (IOException e) { * e.printStackTrace(); } } * * } */
示例3
import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> fileListsList = new ArrayList<String>(); fileListsList.add("D:/zip/device-2016-03-26-195134.png"); fileListsList.add("D:/demo/hyasset/config.xml"); fileListsList.add("D:/demo.txt"); fileListsList.add("D:/demosvn/YCCitizen/build/intermediates/dex-cache/"); try { ZipUtils.zip(fileListsList, "D:/ziped/abc.zip"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* try { ZipUtils.zip("D:/zip/", "D:/ziped/abc.zip"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ } }
- JAVA基础 19.Charset
-
发表日期:2022-08-05 16:24:36 | 来源: | 分类:JAVA基础
-
示例1
public class CharsetDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(System.getProperty("file.encoding"));//GBK } }
- JAVA基础 20.List
-
发表日期:2022-08-05 16:31:29 | 来源: | 分类:JAVA基础
-
示例1
import java.util.ArrayList; import java.util.Collection; import java.util.List; public class ArrayListDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub List<String> allList = new ArrayList<String>(); allList.add("hello"); allList.add("world"); allList.add(1,"my");//在第二个位置上添加内容 System.out.println(allList); Collection<String> allCollection = new ArrayList<String>(); allCollection.add("hello"); allCollection.add("world"); //allCollection.add(1," ");//错误i System.out.println(allCollection); allList.addAll(allCollection);//可以指定位置 System.out.println(allList); allList.remove("world");//根据内容删除 但是只删除第一个 System.out.println(allList); allList.remove(0);//根据内容删除 但是只删除第一个 System.out.println(allList); System.out.println("allList的长度为:"+allList.size()); for (int i = 0; i < allList.size(); i++) { System.out.print(allList.get(i)+"、"); } String arr[] = allList.toArray(new String[]{});//将list 对象转换为 array 对象 } }
示例2
import java.util.Set; import java.util.TreeSet; class Person implements Comparable<Person> { private int age; private String name; public Person(String name, int age) { setName(name); setAge(age); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int compareTo(Person person) { if (this.age > person.age) { return 1; } else if (this.age < person.age) { return -1; } else { //return 0; return this.name.compareTo(person.name); } } public String toString() { return "姓名:" + getName() + " 年龄:" + getAge(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } public class TreeSetDemo { public static void main(String[] args) { // TODO 自动生成的方法存根 Set<Person> treeSet = new TreeSet<Person>(); treeSet.add(new Person("张三", 30)); treeSet.add(new Person("李四", 40)); treeSet.add(new Person("王二", 20)); treeSet.add(new Person("赵六", 60)); treeSet.add(new Person("麻子", 30)); treeSet.add(new Person("李四", 13)); treeSet.add(new Person("赵六", 60)); System.out.println(treeSet); //return 0; //[姓名:李四 年龄:13, 姓名:王二 年龄:20, 姓名:张三 年龄:30, 姓名:李四 年龄:40, 姓名:赵六 年龄:60] /** * 麻子 30 不见了 * 李四 13、40 * 赵六去重复了 */ //[姓名:李四 年龄:13, 姓名:王二 年龄:20, 姓名:张三 年龄:30, 姓名:麻子 年龄:30, 姓名:李四 年龄:40, 姓名:赵六 年龄:60] //赵六去重复了只有赵六去重复了 //[姓名:李四 年龄:13, 姓名:王二 年龄:20, 姓名:张三 年龄:30, 姓名:麻子 年龄:30, 姓名:李四 年龄:40, 姓名:赵六 年龄:60] } }