今天看了一些git的用法,学了一些sql语句,看了一些javafx的使用。
javaIO流
文件读取
文件输入流 FileInputStream
作用是:读取磁盘文件或网络的数据
构造方法:
FileInputStream(File file)
FileInputStream(String filepath)
主要方法:
int read(byte[] buffer) 读取文件数据,保存到字节数组中,返回值是读取长度-1代表读取完毕。
void close() 关闭文件流
try-with-resource
IO流使用完后必须关闭,否则文件流无法得到释放,close方法调用要写在finally中。
为简化关闭流的代码,Java1.7引入了try-with-resource语法,让资源在try-catch完毕后自动关闭。
语法:
try(类 对象 = new 类()){
可能出现异常的代码
}catch(异常类型 对象){
处理异常的代码
}
/**
* 读取文件内容
* @param filepath
*/
public void read(String filepath){
//1、创建文件输入流
try(FileInputStream fis = new FileInputStream(filepath)) {
//2、创建byte数组
byte[] buffer = new byte[1024];
//3、循环调用read方法读取数据,存入byte数组
int len = 0;
while((len = fis.read(buffer)) != -1){
//4、操作byte数组,如果是文字将byte[]转换为String
String str = new String(buffer,0,len,"UTF-8");
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testRead(){
read("C:/xpp/Test6.java");
}
文件写入
文件输出流 FileOutputStream
作用是:将数据写入磁盘文件或网络
构造方法:
FileOutputStream(File file)
FileOutputStream(String filepath)
主要方法:
write(byte[] buffer,int offset,int length) 写入文件,offset开始位置,length写入长度
write(byte[] buffer)
close() 关闭流
/**
* 写入磁盘文件
* @param filepath
* @param content
*/
public void write(String filepath,String content){
//1、创建文件输出流
try (FileOutputStream fos = new FileOutputStream(filepath)){
//2、将字符串转换为byte数组3、调用write写入文件
fos.write(content.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testWrite(){
write("C:/xpp/xxxx.txt","这是我要写入的内容:Hello JavaEE!");
}
有效的数独
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。
示例 1:
输入:board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true
示例 2:
输入:board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
提示:
board.length == 9
board[i].length == 9
board[i][j] 是一位数字(1-9)或者 '.'
class Solution {
public boolean isValidSudoku(char[][] board) {
boolean[][] row = new boolean[9][9];// 记录某行,某位数字是否已经被摆放
boolean[][] col = new boolean[9][9];// 记录某列,某位数字是否已经被摆放
boolean[][] block = new boolean[9][9]; // 记录某 3x3 宫格内,某位数字是否已经被摆放
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '1';
int blockIndex = i / 3 * 3 + j / 3;
if (row[i][num] || col[j][num] || block[blockIndex][num]) {
return false;
} else {
row[i][num] = true;
col[j][num] = true;
block[blockIndex][num] = true;
}
}
}
}
return true;
}
}