В рамках упражнения я разрабатываю код, который будет читать файл и проверять, есть ли в нем числа с плавающей запятой или нет. Я уже написал код, но не уверен, правильно ли я получаю доступ к файлу или есть лучший способ.
Приложение:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Question1 {
/* Write a method public ArrayList readValues(String filename) throws ... that reads a file
* containing floating-point numbers. Throw appropriate exceptions if the file could not be opened
* or if some of the inputs are not floating-point numbers.
*/
private static final String RESOURCE_BASE_PATH = "src/test/resources/";
//under src/main/ java : java_impatient.chapter4.Question1
public static List readValues(String filename) throws IOException {
ArrayList values = new ArrayList();
try (BufferedReader fileReader = new BufferedReader(new FileReader(filename))) {
//TODO : Complete this with TDD ?
}
return values;
}
}
Тест:
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.io.*;
import java.util.List;
public class Question1Test {
//Some other test code here.
//under src/test : testjava_impatient.chapter4.Question1Test
@Test
public void returnsEmptyListForEmptyFile() throws Exception {
//Is this the best way to get the full path of a file?
List values = Question1.readValues( getFilePath("src/test/resources/chapter4/question1_empty.txt") );
Assert.assertTrue(values.isEmpty(), "List is not empty as expected!");
}
private static String getFilePath(String relativePath){
File file = new File(RESOURCE_BASE_PATH + relativePath);
String fullPath = file.getAbsolutePath();
return fullPath;
}
}
Подробнее здесь: https://stackoverflow.com/questions/606 ... en-project