本文目錄一覽:
- 1、java中怎樣從文件中讀取數據?
- 2、如何在java 中讀取資料庫的數據
- 3、java中怎麼從文件中讀取數
- 4、java如何從資料庫讀取數據並寫入txt文件?
- 5、java如何從串口讀取數據帶GUI
java中怎樣從文件中讀取數據?
分為讀位元組,讀字元兩種讀法\x0d\x0a◎◎◎FileInputStream 位元組輸入流讀文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0a\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File(“G:\\just for fun\\xiangwei.txt”);\x0d\x0a\x0d\x0aFileInputStream fin=new FileInputStream(f);\x0d\x0a\x0d\x0abyte[] bs=new byte[1024];\x0d\x0a\x0d\x0aint count=0;\x0d\x0awhile((count=fin.read(bs))0)\x0d\x0a{\x0d\x0a\x0d\x0aString str=new String(bs,0,count);//反覆定義新變數:每一次都 重新定義新變數,接收新讀取的數據\x0d\x0a\x0d\x0aSystem.out.println(str);//反覆輸出新變數:每一次都 輸出重新定義的新變數\x0d\x0a}\x0d\x0afin.close();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a◎◎◎FileReader 字元輸入流讀文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File(“H:\\just for fun\\xiangwei.txt”);\x0d\x0a\x0d\x0aFileReader fre=new FileReader(f);\x0d\x0a\x0d\x0aBufferedReader bre=new BufferedReader(fre);\x0d\x0a\x0d\x0aString str=””;\x0d\x0awhile((str=bre.readLine())!=null)//●判斷最後一行不存在,為空\x0d\x0a{\x0d\x0aSystem.out.println(str);\x0d\x0a}\x0d\x0abre.close();\x0d\x0a fre.close();\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}
如何在java 中讀取資料庫的數據
讀取資料庫最基礎的可以使用JDBC連接資料庫讀取數據
jdbc方式連接資料庫查詢數據:
當然也有其他的方式 比如Hibernate\mybatis\ibatis\jpa等等 架構都可以 這你可以後面去查詢資料學習
你可以先看JDBC吧
java中怎麼從文件中讀取數
1.package txt;
2.
3.import java.io.BufferedReader;
4.import java.io.File;
5.import java.io.FileInputStream;
6.import java.io.InputStreamReader;
7.
8./**
9. * 讀取TXE數據
10. */
11.public class ReadTxtUtils {
12. public static void main(String arg[]) {
13. try {
14. String encoding = “GBK”; // 字元編碼(可解決中文亂碼問題 )
15. File file = new File(“c:/aa.txt”);
16. if (file.isFile() file.exists()) {
17. InputStreamReader read = new InputStreamReader(
18. new FileInputStream(file), encoding);
19. BufferedReader bufferedReader = new BufferedReader(read);
20. String lineTXT = null;
21. while ((lineTXT = bufferedReader.readLine()) != null) {
22. System.out.println(lineTXT.toString().trim());
23. }
24. read.close();
25. }else{
26. System.out.println(“找不到指定的文件!”);
27. }
28. } catch (Exception e) {
29. System.out.println(“讀取文件內容操作出錯”);
30. e.printStackTrace();
31. }
32. }
33.}
java讀取TXT文件中的數據,每一行就是一個數,返回一個數組,代碼?
?
List list=new ArrayList();
BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File(“in.txt”))));
String str=null;
while((str=br.readLine())!=null)
{
list.add(new Integer(str));
}
Integer[] i=new Integer[list.size()];
list.toArray(i);
TXT文本中如據形如:
123
456
789
讀入二維數組效果為:
temp[0][]={1,2,3};
temp[1][]={4,5,6};
temp[2][]={7,8,9};
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.*;
public class xx{
public static void main(String[]args){
String s;
int[][]save=new int[3][3];
try{
BufferedReader in =new BufferedReader(new FileReader(“C:\\txt.txt”));
int i=0;
while((s=in.readLine())!=null){
save[i][0]=Integer.parseInt(s.substring(0,1));
save[i][1]=Integer.parseInt(s.substring(1,2));
save[i][2]=Integer.parseInt(s.substring(2,3));
i++;
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
for(int i=0;i3;i++)
{
for(int j=0;j3;j++){
System.out.print(save[i][j]);
}
System.out.println();
}
}
}
或
?
BufferedReader bf=new BufferedReader(new FileReader(“Your file”));
String lineContent=null;
int i = 0;
int [][] temp = new int [3][];
while((lineContent=bf.readLine())!=null){
String [] str = lineContent.split(“\\d”);// 將 lineContent 按數字拆分
for(int j = 0; j str.length(); j++){
int [i][j] = Integer.parseInt(str[j]);
}
i++;
}
scp|cs|ff|201101
這是d:\\a.txt的數據,與「|」分割取數據出來,保存在變數a;b;c;d里
import java.io.*;
public class Test{
public static void main(String[] args)throws Exception{
String a, b, c, d;
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(“d:\\a.txt”));
String s = br.readLine();
while(s != null){
sb.append(s);
s = br.readLine();
}
s = sb.toString();
String[] str = s.split(“|”);
a = str[0];
b = str[0];
c = str[0];
d = str[0];
}
}
java如何從資料庫讀取數據並寫入txt文件?
寫Java程序時經常碰到要讀如txt或寫入txt文件的情況,但是由於要定義好多變數,經常記不住,每次都要查,特此整理一下,簡單易用,方便好懂!
[java] view plain copy
package edu.thu.keyword.test;
import java.io.File;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
public class cin_txt {
static void main(String args[]) {
try { // 防止文件建立或讀取失敗,用catch捕捉錯誤並列印,也可以throw
/* 讀入TXT文件 */
String pathname = “D:\\twitter\\13_9_6\\dataset\\en\\input.txt”; // 絕對路徑或相對路徑都可以,這裡是絕對路徑,寫入文件時演示相對路徑
File filename = new File(pathname); // 要讀取以上路徑的input。txt文件
InputStreamReader reader = new InputStreamReader(
new FileInputStream(filename)); // 建立一個輸入流對象reader
BufferedReader br = new BufferedReader(reader); // 建立一個對象,它把文件內容轉成計算機能讀懂的語言
String line = “”;
line = br.readLine();
while (line != null) {
line = br.readLine(); // 一次讀入一行數據
}
/* 寫入Txt文件 */
File writename = new File(“.\\result\\en\\output.txt”); // 相對路徑,如果沒有則要建立一個新的output。txt文件
writename.createNewFile(); // 創建新文件
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write(“我會寫入文件啦\r\n”); // \r\n即為換行
out.flush(); // 把緩存區內容壓入文件
out.close(); // 最後記得關閉文件
} catch (Exception e) {
e.printStackTrace();
}
}
}
java如何從串口讀取數據帶GUI
1.導入支持java串口通信的jar包:
在maven項目的pom.xml中添加RXTXcomm的依賴 或者 下載RXTXcomm.jar並導入到項目中。
支持Java串口通信操作的jar包,java.comm比較老,而且不支持64位系統,推薦使用Rxtx這個jar包(32位/64位均支持)。
注意:運行過程中拋出 java.lang.UnsatisfiedLinkError 錯誤或 gnu.io 下的類找不到時,將rxtx解壓包中的 rxtxParallel.dll,rxtxSerial.dll 這兩個文件複製到 C:\Windows\System32 目錄下可解決該錯誤。
2.編寫代碼操作串口:
串口必要參數類:包含連接串口所必須的參數,方便在調用串口時設置和傳遞串口參數。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/154430.html