本文提供全面的Java学习路线,涵盖基础开发环境搭建、核心语法、面向对象编程、集合框架、异常处理至深入的IO操作与实践应用,从入门到进阶,助你掌握Java编程精髓,实现高效编程实践。
Java基础知识
1. Java开发环境搭建
确保电脑上安装了Java Development Kit (JDK),推荐下载最新版本。通过命令行输入java -version
来确认已安装并查看JDK版本。
# 在Ubuntu环境下安装JDK
sudo apt-get update
sudo apt-get install openjdk-11-jdk
java -version
配置环境变量,便于命令行直接运行Java程序:
# 修改bash配置文件以添加JDK路径
echo 'export PATH=$PATH:/usr/lib/jvm/java-11-openjdk-amd64/bin' >> ~/.bashrc
source ~/.bashrc
2. 基础语法
Java基础语法覆盖了变量、数据类型、运算符、流程控制等概念。
public class HelloWorld {
public static void main(String[] args) {
String greeting = "Hello, World!";
int age = 30;
boolean isStudent = true;
System.out.println(greeting);
System.out.println(age);
System.out.println(isStudent);
}
}
上述代码展示了如何定义和使用字符串、整数和布尔类型变量,并通过System.out.println()
进行输出。
3. 运算符和类型转换
Java提供了多种运算符,包括算术、比较等。
public class Operators {
public static void main(String[] args) {
int a = 5;
int b = 3;
int sum = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;
double divide = (double) a / b; // 强制类型转换
System.out.println("Sum: " + sum);
System.out.println("Sub: " + sub);
System.out.println("Mul: " + mul);
System.out.println("Div: " + div);
System.out.println("Double Div: " + divide);
}
}
注意,在执行除法运算时,结果可能为整数,上述代码通过类型转换确保了浮点数结果。
4. 流程控制
流程控制结构如条件判断和循环对于有效编程至关重要。
public class ControlFlow {
public static void main(String[] args) {
int num = 10;
if (num > 0) {
System.out.println("Number is positive.");
} else if (num < 0) {
System.out.println("Number is negative.");
} else {
System.out.println("Number is zero.");
}
for (int i = 0; i < 5; i++) {
System.out.println("Count: " + i);
}
}
}
集合框架
1. List
列表用于存储有序元素。
import java.util.ArrayList;
public class ListExample {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
2. Set
集合用于存储不重复的元素。
import java.util.HashSet;
public class SetExample {
public static void main(String[] args) {
HashSet<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
3. Map
映射存储键值对,方便通过键获取特定值。
import java.util.HashMap;
public class MapExample {
public static void main(String[] args) {
HashMap<String, String> fruitMap = new HashMap<>();
fruitMap.put("Apple", "Fruit");
fruitMap.put("Banana", "Fruit");
fruitMap.put("Cherry", "Fruit");
System.out.println("Find Apple: " + fruitMap.get("Apple"));
}
}
异常处理
public class ExceptionHandling {
public static void main(String[] args) {
try {
int[] numbers = new int[5];
numbers[5] = 10; // 将导致数组越界异常
System.out.println(numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Array index out of bounds: " + e.getMessage());
}
}
}
IO操作
1. 文件读写
import java.io.*;
public class FileIO {
public static void main(String[] args) {
try {
File file = new File("example.txt");
FileWriter writer = new FileWriter(file);
writer.write("Hello, this is a test file.");
writer.close();
FileReader reader = new FileReader(file);
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch);
}
reader.close();
} catch (IOException e) {
System.err.println("Error with file I/O: " + e.getMessage());
}
}
}
2. 常见流使用
import java.io.*;
public class StreamUsage {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println("Reading: " + line);
writer.write(line);
writer.newLine();
}
System.out.println("Writing complete.");
} catch (IOException e) {
System.err.println("Error with stream handling: " + e.getMessage());
}
}
}
实践项目
创建一个简易的文本编辑器,包括基本的文件读写、打开和保存功能。
基础代码框架
import java.io.*;
public class SimpleEditor {
public static void main(String[] args) {
File file = new File("example.txt");
try {
if (!file.createNewFile()) {
FileEditor.read(file);
} else {
FileEditor.write(file, "This is a new document.");
}
} catch (IOException e) {
System.err.println("Error with file handling: " + e.getMessage());
}
}
}
class FileEditor {
public static void read(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
public static void write(File file, String content) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
}
}
完整实现代码
import java.io.*;
public class SimpleEditor {
public static void main(String[] args) {
File file = new File("example.txt");
try {
if (!file.createNewFile()) {
FileEditor.read(file);
} else {
FileEditor.write(file, "This is a new document.");
}
} catch (IOException e) {
System.err.println("Error with file handling: " + e.getMessage());
}
}
}
class FileEditor {
public static void read(File file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
public static void write(File file, String content) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
}
}
通过上述提供全面的代码示例和实践指导,你将能更深入地理解和应用Java编程技术。