//string goodBarcodeFile = "goodBarcodes.txt"; //string badBarcodeFile = "badBarcodes.txt"; //List<string> goodBarcodeList = new List<string>(); //List<string> badBarcodeList = new List<string>();代码基础上修改,获取本地文件中的良品和不良品条码
时间: 2024-02-06 11:04:28 浏览: 36
好的,你可以按照以下代码来获取本地文件中的良品和不良品条码:
```
using System.IO;
using System.Collections.Generic;
string goodBarcodeFile = "goodBarcodes.txt";
string badBarcodeFile = "badBarcodes.txt";
List<string> goodBarcodeList = new List<string>();
List<string> badBarcodeList = new List<string>();
// 获取良品条码
if (File.Exists(goodBarcodeFile))
{
using (StreamReader sr = new StreamReader(goodBarcodeFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
goodBarcodeList.Add(line);
}
}
}
// 获取不良品条码
if (File.Exists(badBarcodeFile))
{
using (StreamReader sr = new StreamReader(badBarcodeFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
badBarcodeList.Add(line);
}
}
}
```
上述代码首先判断了良品和不良品条码文件是否存在,如果存在,则逐行读取文件内容并添加到对应的 List 中。其中,使用了 `StreamReader` 类来读取文件内容。读取完成后,`goodBarcodeList` 和 `badBarcodeList` 分别存储了良品和不良品条码列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
package ece448.iot_sim; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ece448.iot_sim.http_server.RequestHandler; public class HTTPCommands implements RequestHandler { // Use a map so we can search plugs by name. private final TreeMap<String, PlugSim> plugs = new TreeMap<>(); public HTTPCommands(List plugs) { for (PlugSim plug: plugs) { this.plugs.put(plug.getName(), plug); } } @Override public String handleGet(String path, Map<String, String> params) { // list all: / // do switch: /plugName?action=on|off|toggle // just report: /plugName logger.info("HTTPCmd {}: {}", path, params); if (path.equals("/")) { return listPlugs(); } PlugSim plug = plugs.get(path.substring(1)); if (plug == null) return null; // no such plug String action = params.get("action"); if (action == null) return report(plug); // P2: add your code here, modify the next line if necessary if("toggle".equals(action)){ plug.toggle(); return report(plug); }else if("on".equals(action)){ plug.switchOn(); return report(plug); }else if("off".equals(action)){ plug.switchOff(); return report(plug); } return "<html><body></body></html>"; } protected String listPlugs() { StringBuilder sb = new StringBuilder(); sb.append("<html><body>"); for (String plugName: plugs.keySet()) { sb.append(String.format("%s", plugName, plugName)); } sb.append("</body></html>"); return sb.toString(); } protected String report(PlugSim plug) { String name = plug.getName(); return String.format("<html><body>" +"Plug %s is %s." +"Power reading is %.3f." +"Switch On" +"Switch Off" +"Toggle" +"</body></html>", name, plug.isOn()? "on": "off", plug.getPower(), name, name, name); }如何对上述代码全部功能进行测试呢?请提供测试代码。
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)