summaryrefslogtreecommitdiff
path: root/java/client/src/test/java/org/apache/qpid/example/shared/FileUtils.java
blob: 6f33727194a51e27584a543322267b463f86cb9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package org.apache.qpid.example.shared;

import java.io.*;

/**
 * Class that provides file related utility methods for utility use
 * Author: Marnie McCormack
 * Date: 20-Jul-2006
 * Time: 08:17:16
 * Copyright JPMorgan Chase 2006
 */
public class FileUtils {


    //Reads file content into String
    public static String getFileContent(String filePath) throws IOException
    {

        BufferedReader reader = null;
        String tempData = "";
        String eol = "\n\r";

        try
        {
            String line;
            reader = new BufferedReader(new FileReader(filePath));
            while ((line = reader.readLine()) != null)
            {
                if (!tempData.equals(""))
                {
                    tempData = tempData + eol + line;
                }
                else
                {
                    tempData = line;
                }
            }
        }
        finally
        {
            if (reader != null)
            {
                reader.close();
            }
        }
        return tempData;
    }

     /*
     * Reads xml from a file and returns it as an array of chars
     */
    public static char[] getFileAsCharArray(String filePath) throws IOException
    {
        BufferedReader reader = null;
        char[] tempChars = null;
        String tempData = "";

        try
        {
            String line;
            reader = new BufferedReader(new FileReader(filePath));
            while ((line = reader.readLine()) != null)
            {
                tempData = tempData + line;
            }
            tempChars = tempData.toCharArray();
        }
        finally
        {
            if (reader != null)
            {
                reader.close();
            }
        }
        return tempChars;
    }

    /*
    * Write String content to filename provided
    */
    public static void writeStringToFile(String content, String path) throws IOException
    {

        BufferedWriter writer = new BufferedWriter(new FileWriter(new File(path)));
        writer.write(content);
        writer.flush();
        writer.close();
    }

    /*
    * Allows moving of files to a new dir and preserves the last bit of the name only
    */
    public static void moveFileToNewDir(String path, String newDir) throws IOException
    {
        //get file name from current path
        //while more files in dir publish them
        File pathFile = new File(path);
        if (pathFile.isDirectory())
        {
            File[] files = pathFile.listFiles();
            for (File file : files)
            {
                moveFileToNewDir(file,newDir);
            }
        }
    }

    /*
    * Allows moving of a file to a new dir and preserves the last bit of the name only
    */
    public static void moveFileToNewDir(File fileToMove, String newDir) throws IOException
    {
        moveFile(fileToMove,getArchiveFileName(fileToMove,newDir));
    }

    /*
    * Moves file from a given path to a new path with String params
    */
    public static void moveFile(String fromPath, String dest) throws IOException
    {
        moveFile(new File(fromPath),new File(dest));
    }

    /*
    * Moves file from a given path to a new path with mixed params
    */
    public static void moveFile(File fileToMove, String dest) throws IOException
    {
        moveFile(fileToMove,new File(dest));
    }

    /*
    * Moves file from a given path to a new path with File params
    */
    public static void moveFile(File fileToMove, File dest) throws IOException
    {
        fileToMove.renameTo(dest);
    }

    /*
    * Deletes a given file
    */
    public static void deleteFile(String filePath) throws IOException
    {
        new File(filePath).delete();
    }

    private static String getArchiveFileName(File fileToMove, String archiveDir)
    {
         //get file name from current path
        String fileName = fileToMove.getName();
        return archiveDir + File.separator + fileName;
    }
}