response =
File("src\\test\\java\\com\\resources\\products\\response.json").bufferedReader()
.use { it.readText() }
I have this line of code for mocking response and it is working fine on pc but throws not found on macbook? Any solutions? Thanks.
You can create assets directory in unit test dir(java unit test), and put your response.json into it. and then you can read this file with these code below:
private static File readAssetsFile(String fileName) {
// create file for assets file
final String dir = System.getProperty("user.dir");
File assetsDir = new File(dir, File.separator + "src/test/assets/");
return new File(assetsDir, fileName);
}
private static String streamToString(InputStream inputStream)
throws IOException {
if (inputStream == null) {
return "";
}
StringBuilder sBuilder = new StringBuilder();
String line;
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
while ((line = bufferedReader.readLine()) != null) {
sBuilder.append(line).append("\n");
}
return sBuilder.toString();
}
// YOUR TESTCASE
#Test
public void testReadResponseJson() throws IOException {
File respFile = readAssetsFile("response.json");
String respText = streamToString(new FileInputStream(respFile));
assertEquals("{}", respText);
}
I have solved with:
val home = System.getProperty("user.home")
val file = File(home + File.separator + "Desktop" + File.separator + "response.json")
Desktop directory for the sample. I will give the absolute path step by step.
Related
I am looking to create a .tar.gz file of the following folder/directory structure, while retaining the same folder/ directory structure
ParentDir\
ChildDir1\
-file1
ChildDir2\
-file2
-file3
ChildDir3\
-file4
-file5
However I am only able to create a .tar.gz of all the files, without the folder/directory structure.
ie: ParentDir.tar.gz
ParentDir\
-file1
-file2
-file3
-file4
-file5
Using the user accepted answer in Compress directory to tar.gz with Commons Compress, I have the following code:
public void exportStaticFilesTar(String appID) throws, Exception {
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
GzipCompressorOutputStream gzOut = null;
TarArchiveOutputStream tOut = null;
try {
String filename = "ParentDir.tar.gz"
//"parent/childDirToCompress/"
String path = "<path to ParentDir>";
fOut = new FileOutputStream(new File(filename));
bOut = new BufferedOutputStream(fOut);
gzOut = new GzipCompressorOutputStream(bOut);
tOut = new TarArchiveOutputStream(gzOut);
addFileToTarGz(tOut, path, "");
} catch (Exception e) {
log.error("Error creating .tar.gz: " +e);
} finally {
tOut.finish();
tOut.close();
gzOut.close();
bOut.close();
fOut.close();
}
//Download the file locally
}
private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
File f = new File(path);
String entryName = base + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.putArchiveEntry(tarEntry);
if (f.isFile()) {
IOUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
} else {
tOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
}
}
}
}
Can someone please advice, how I can modify the current code to retain the folder/directory structure when creating a .tar.gz file. Thanks!
I get on better with the following. Important: you need to pass as base the correct initial value which is the first directory in the tree (excluding the fs root):
private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
File f = new File(path);
String entryName = base + File.separatorChar + f.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
tOut.putArchiveEntry(tarEntry);
if (f.isFile()) {
IOUtils.copy(new FileInputStream(f), tOut);
tOut.closeArchiveEntry();
} else {
tOut.closeArchiveEntry();
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
addFileToTarGz(tOut, child.getAbsolutePath(), entryName);
}
}
}
}
Using the code mentioned in the request, I was able to generate a successful compressed file on my linux system.
You can replicate by changing user path and the root folder which needs to be compressed.
public static void createTarGZ() throws FileNotFoundException, IOException
{
String sourceDirectoryPath = null;
String destinationTarGzPath = null;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
GzipCompressorOutputStream gzOutpuStream = null;
TarArchiveOutputStream tarArchiveOutputStream = null;
String basePath = "/home/saad/CompressionTest/";
try
{
sourceDirectoryPath = basePath + "asterisk";
destinationTarGzPath = basePath + "asterisk.tar.gz";
fileOutputStream = new FileOutputStream(new File(destinationTarGzPath));
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
gzOutpuStream = new GzipCompressorOutputStream(bufferedOutputStream);
tarArchiveOutputStream = new TarArchiveOutputStream(gzOutpuStream);
addFileToTarGz(tarArchiveOutputStream, sourceDirectoryPath, "");
}
finally
{
tarArchiveOutputStream.finish();
tarArchiveOutputStream.close();
gzOutpuStream.close();
bufferedOutputStream.close();
fileOutputStream.close();
}
}
private static void addFileToTarGz(TarArchiveOutputStream tarOutputStream, String path, String base) throws IOException
{
File fileToCompress = new File(path);
String entryName = base + fileToCompress.getName();
TarArchiveEntry tarEntry = new TarArchiveEntry(fileToCompress, entryName);
tarOutputStream.putArchiveEntry(tarEntry);
// If its a file, simply add it
if (fileToCompress.isFile())
{
IOUtils.copy(new FileInputStream(fileToCompress), tarOutputStream);
tarOutputStream.closeArchiveEntry();
}
// If its a folder then add all its contents
else
{
tarOutputStream.closeArchiveEntry();
File[] children = fileToCompress.listFiles();
if (children != null)
{
// add every file/folder recursively to the tar
for (File child : children)
{
addFileToTarGz(tarOutputStream, child.getAbsolutePath(), entryName + "/");
}
}
}
}
I have a method called "add" which takes a string as an argument and uses the bufferedwriter to write it to the file. Once this is done, the bufferedwriter is flushed.
In another method "read", I loop through the lines in the file, but the lines are null (hence I cannot print them).
When I call "read" inside "add", I can print the lines nonetheless.
public String add(String data) throws IOException{
this.bufferedWriter.write(data);
this.bufferedWriter.flush();
//this.read(data);
logger.info("written " +data);
return data;
}
public String read(String key) throws IOException{
logger.info("start reading ...: ");
String line = this.bufferedReader.readLine();
while (line!= null) {
logger.info("loop start reading ...: "+line);
if(line.split(" ").equals(key)) {
logger.info("reading line: "+line);
return line;
}
line = this.bufferedReader.readLine();
}
return "F"; // indicates the key is not in the storage
}
This is the full code:
public class FileManager {
Path dataDir;
File f;
FileReader fileReader;
BufferedReader bufferedReader;
FileWriter fileWriter;
BufferedWriter bufferedWriter;
public static Logger logger = Logger.getLogger(FileManager.class.getName());
public FileManager(Path dataDir) throws IOException {
logger.info("in file manager: ");
this.dataDir = dataDir;
String dirName = dataDir.toString();
String fileName = "fifo2.txt";
File dir = new File (dirName);
dir.mkdirs();
f = new File (dir, fileName);
logger.info("file established at "+f.getAbsolutePath());
if(!f.exists()){
logger.info("file not there so create new one ");
f.createNewFile();
logger.info("file created!!! ");
}else{
logger.info("file already exists");
System.out.println("File already exists");
}
logger.info("file stage complete");
this.fileReader = new FileReader(f);
this.bufferedReader = new BufferedReader(fileReader);
this.fileWriter = new FileWriter(f, true);
this.bufferedWriter = new BufferedWriter(fileWriter);
}
public String add(String data) throws IOException{
this.bufferedWriter.write(data);
this.bufferedWriter.flush();
//this.read(data);
logger.info("written " +data);
return data;
}
public String read(String key) throws IOException{
logger.info("start reading ...: ");
String line = this.bufferedReader.readLine();
while (line!= null) {
logger.info("loop start reading ...: "+line);
if(line.split(" ").equals(key)) {
logger.info("reading line: "+line);
return line;
}
line = this.bufferedReader.readLine();
}
return "F"; // indicates the key is not in the storage
}
public String delete(String key) throws IOException{
logger.info("Entering deletion in file storage");
String line;
while ((line = this.bufferedReader.readLine()) != null) {
if(line.split(" ").equals(key)) {
line = "DELETED";
logger.info("del_reading line: "+line);
bufferedWriter.write(line);
}
line = this.bufferedReader.readLine();
}
return "F"; // indicates the key to be deleted is not in the storage
}
}
What you should try to do is to create a new instance of BufferedReader/Writer each time you do a read/write operation with the file. Make sure to flush and close after each use.
I want to access the certificate file what should be the path if that file is in resources/certs/ folder.
I tried it with the classpath put still getting the exception FileNotFound.
what should be path for that I should specify for this in my application.properties.
if you want to read a file, you can use this code (fileName: "/certs/xxx")
public String readFile(String fileName) throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
StringBuilder resultStringBuilder = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
resultStringBuilder.append(line).append("\n");
}
}
return resultStringBuilder.toString();
}
I can't figure out how to reference a specific line of text in a txt file. I need a specific image from a specific list of URL's and I am trying to generate the URL's by concatenating a URL prefix and a search number from a list of numbers in a txt file. I can't figure out how to reference the txt file and get a string from a line number.
package getimages;
public class ExtractAllImages {
public static int url_to_get = 1;
public static String urlupc;
public static String urlpre = "http://urlineedimagefrom/searchfolder/";
public static String url2 = "" + urlpre + urlupc + "";
public static void main(String args[]) throws Exception {
while(url_to_get > 2622){
String line = Files.readAllLines(Paths.get("file_on_my_desktop.txt")).get(url_to_get);
urlupc = line;
url2 = "" + urlpre + urlupc + "";
String webUrl = url2;
URL url = new URL(webUrl);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
parser.parse(br, callback, true);
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
AttributeSet attributes = iterator.getAttributes();
String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);
if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
try {
downloadImage(webUrl, imgSrc);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
}
public static String right(String value, int length) {
return value.substring(value.length() - length);}
private static void downloadImage(String url, String imgSrc) throws IOException {
BufferedImage image = null;
try {
if (!(imgSrc.startsWith("http"))) {
url = url + imgSrc;
} else {
url = imgSrc;
}
String webUrl = url2;
String imagename = right(webUrl , 12);
imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
String imageFormat = null;
imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
String imgPath = null;
imgPath = "C:/Users/Noah/Desktop/photos/" + urlupc + ".jpg";
URL imageUrl = new URL(url);
image = ImageIO.read(imageUrl);
if (image != null) {
File file = new File(imgPath);
ImageIO.write(image, imageFormat, file);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
My error is in setting the String line, My txt file has 2622 lines and I can't reference the file on my desktop and im not sure how to set the file path to my desktop? Sorry I'm not good at java.
Thanks for any help.
From what I understood you don't know how to access a file that is on your Desktop. Try doing this:
String path = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\" + fileName + ".txt";
Let me explain. We are using
System.getProperty("user.name")
to get the name of the user, if you don't want to use it then you can replace it with the name of your user. Then we are using
"\\Desktop\\"
to acess the Desktop, and finally we are adding the
fileName + ".txt"
to access the file we want that has the extension '.txt'.
I have to read a file using servlet.here is the code iam using.but file is not reading using this code.Always printing File contains null value-----------------:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
String filename = "D/root.properties";
ServletContext context = getServletContext();
InputStream inp = context.getResourceAsStream(filename);
if (inp != null) {
InputStreamReader isr = new InputStreamReader(inp);
BufferedReader reader = new BufferedReader(isr);
PrintWriter pw = response.getWriter();
String text = "";
while ((text = reader.readLine()) != null) {
}
} else {
System.out.println("File contains null value-----------------");
}
} catch(Exception e) {
System.out.println("Rxpn............................................."+e);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
javadoc to the rescue :
java.net.URL getResource(java.lang.String path)
throws java.net.MalformedURLException
Returns a URL to the resource that is mapped to the given path.
The path must begin with a / and is interpreted as relative to the current context root, or relative to the /META-INF/resources directory
of a JAR file inside the web application's /WEB-INF/lib directory.
This method will first search the document root of the web application
for the requested resource, before searching any of the JAR files
inside /WEB-INF/lib. The order in which the JAR files inside
/WEB-INF/lib are searched is undefined.
If you want to read from a resource in the web app, use a path as indicated above. If you want to read from the file system, use file IO (and the correct file name): new FileInputStream("D:/root.properties")
Use following code.
With this you can read file
File file = new File("Filepath");
try {
if (file.exists()) {
BufferedReader objBufferReader = new BufferedReader(
new FileReader(file));
ArrayList<String> arrListString = new ArrayList<String>();
String sLine = "";
int iCount = 0;
while ((sLine = objBufferReader.readLine()) != null) {
arrListString.add(sLine);
}
objBufferReader.close();
for (iCount = 0; iCount < arrListString.size(); iCount++) {
if (iCount == 0) {
createTable(arrListString.get(iCount).trim());
} else {
insertIntoTable(arrListString.get(iCount).trim());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream file = new FileInputStream("c:\\hi.txt");
DataInputStream input = new DataInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(input));
String text = "";
while ((text = br.readLine()) != null) {
System.out.println(text);
}
}
}
Please try the above code sample. I think in your code , file is not found. Please give the file path in the above code and try.