gpt4 book ai didi

java - 同时读取 .csv 文件

转载 作者:行者123 更新时间:2023-11-30 11:04:21 26 4
gpt4 key购买 nike

我需要按时间间隔从 4 个单独的 .csv 数据文件中提取数据以同时输出数字。我只能按顺序提取文件。我不确定线程​​是否有帮助,而且我一直无法让它们正确运行。我的最终结果将在 JFrame 中显示输出。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class Inputtest {
public static void main(String[] args) throws IOException, InterruptedException {

O21 a = new O21();
O22 b = new O22();
PH1 c = new PH1();
PH2 d = new PH2();

a.O21();
b.O22();
c.PH1();
d.PH2();
}
}

class O21
{
public void O21() throws FileNotFoundException, InterruptedException {
int e = 21; //preset Level

Scanner O1 = new Scanner(new File("O21.txt"));
O1.useDelimiter(",");
while (O1.hasNext()) {
String a = O1.next();
int aa = Integer.parseInt(a);
Thread.sleep(500); // Time delay for output

if (a.trim().isEmpty()) { continue; }
if(aa > (e + 1)) {
System.out.println(a);
System.out.println("Alarm high");
continue;
}
if(aa < (e-1)) {
System.out.println(a);
System.out.println("Alarm Low");
continue;
}
System.out.println(a);
}
}
}

class O22 {
public void O22() throws FileNotFoundException, InterruptedException {
double f = 20; //preset Level
Scanner O2 = new Scanner(new File("O22.txt"));
O2.useDelimiter(",");
while (O2.hasNext()) {
String b = O2.next();
int bb = Integer.parseInt(b);
Thread.sleep(500); // Time delay for output

if (b.trim().isEmpty()) { continue; }

if(bb > (f + 1)) {
System.out.println(b);
System.out.println("Alarm high");
continue;
}

if (bb < (f - 1)) {
System.out.println(b);
System.out.println("Alarm Low");
continue;
}

System.out.println(b);
}
}
}

class PH1 {
public void PH1() throws FileNotFoundException, InterruptedException {
double g = 7; //preset Level

Scanner P1 = new Scanner(new File("PH1.txt"));
P1.useDelimiter(",");
while (P1.hasNext()) {
String c = P1.next();
double cc = Double.parseDouble(c);
Thread.sleep(1000); // Time delay for output

if (c.trim().isEmpty()) { continue; }
if (cc > (g + .5)) {
System.out.println(c);
System.out.println("Alarm high");
continue;
}

if (cc < (g - .5)) {
System.out.println(c);
System.out.println("Alarm Low");
continue;
}
System.out.println(c);
}
}
}

class PH2 {
public void PH2() throws FileNotFoundException, InterruptedException {
double h = 7; //preset Level

Scanner P2 = new Scanner(new File("PH2.txt"));
P2.useDelimiter(",");
while (P2.hasNext()) {
String d = P2.next();
double dd = Double.parseDouble(d);
Thread.sleep(1000); // Time delay for output
if (d.trim().isEmpty()) { continue; }
if (dd > (h + .5)) {
System.out.println(d);
System.out.println("Alarm high");
continue;
}
if (dd < (h - .5)) {
System.out.println(d);
System.out.println("Alarm Low");
continue;
}

System.out.println(d);
}
}
}

最佳答案

是的,线程会帮助你。很简单,

class O21 implements Runnable
{
@Override public void run() // Used to be your constructor
{
....

在你的 main 方法中:

...
Thread a = new Thread( new O21() );
a.start();
...

O22PH1PH2 也类似,它们现在都将同时执行。

您可以将构造函数添加到您的 Runnable 以将引用传递给您的 JFrame。我会把它留作练习。此外,SO 和其他地方有大量关于线程编程的资源。

干杯,

关于java - 同时读取 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30066439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com